SHRI PAL SINGH

An Enterpreneur With Kind Heart

Blog

PHP Basics Updated

Must Use These Basic Standard During Coding

1) Always use thumbnail according to the requirement of HTML. You can crop or re-size the image using either PHP’s ImageMagick or GD functions to work with images

Example:For JPEG
function resize_image($file, $w, $h, $crop=FALSE)
{
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
}
else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
$img = resize_image(‘/path/to/some/image.jpg’, 200, 200); // Call the function resize_image

2) Always use dynamic URL in Framework (Cakephp , Zend).
Cakephp exp: Always use

$this->Html->url(array(‘controller’=>’bar’,’action’=>’display’));

DON’T Use

$url=DOMAIN_NAME.’/bar/display’;

3) If data is large, then always use php pagination or Ajax pagination. Never use jQuery Pagination Plugin which don’t use ajax.

4) Every validation should be server side on Front end. Never use Js and jQuery validation without ajax.

5) If any form is public then always use captcha.

6) Stay Away from Anything Ending With _once()
We all know that include() simply gives us a warning if it fails, while require() kills the script with a fatal error when it fails. What we don’t forget is that include_once() and require_once() is extremely hard on server resources. There is nothing we can do about it, it’s how PHP is set up. Just remember that these things kill your server resources, specially on a huge framework, and if you plan your code properly you won’t even need it anyway.

7) Develop With Error Reporting On
The very first thing you do when starting a new project is to turn error reporting to E_ALL, and you should only turn it off ten seconds before going to production mode. I do this with every project that I build and there is nothing better than running a project in full production mode and not even getting one error. Besides that, with error reporting on, you pick up any small errors that will eventually grow up to bite you in the… well, you get my point.

8) Use PHP’s Inbuilt Functions
Ok, you want to count the amount of keys in an array? You can loop through the array and simply increment a value for each iteration, right? Or you can just use the built in PHP function count(), which does just what it should. PHP has many built-in functions that can do what you need them to, so check out the manual to make sure you are doing it in the best way possible

9) Use POST Not GET
Ok, this isn’t always possible, but when its really not necessary, don’t use GET, use POST. The reason is simple – GET is simple to emulate, all I need to do is add something to my address bar and I can hack your project. Obviously GET is the easy way to do pagination, but when using form submission especially, stay with POST, it’s safer.

10) Calculate Only Once
Calculate and assign the value to the variable if that value is getting used numerous time rather than calculating it again and again where it is being used.
For example, the following will degrade the performance.
for( $i=0; i< count($arrA); $i++){
echo count($arrA);
}
The script below will perform much better.
$len = count($arrA);
for( $i=0; i< $len; $i++){
echo $len;
}.

11) Always test your code or script step by step during development of any functionality.
On given below example module is broken into sub functionality, Always test each sub functionality during coding separately. It reduces the chance of bug.
Example:

Function functionalityExample(){
functionality part 1// Test1
functionality part 2//Test2
functionality part 3//Test3
}

12) Email Tips
-If you are using any framework then always use its default feature for mailing, never use core php mail on it.
-If you are working on core php and your project have large and important role of mail then try to use PHP mailer library.
https://github.com/PHPMailer/PHPMailer

-Some Important Tips For HTML mail.

    • Don’t use background images in your HTML email.
    • Do not use externally linked CSS.
    • Do not use a single image for your entire message.

(Anti-spam filters are wise to this trick and on top of that, your message size could end up quite large by doing this)

  • Do not design your HTML email like it’s a web page.

Eg: Web browsers are much more sophisticated than email clients when it comes to rendering HTML. If you use DIV tags to define the layout of your HTML email message, do not be surprised if the layout is completely broken when it arrives to your recipients.
So there you have it – all of these things might be part of good HTML design practice when coding for your website, but they can potentially cause problems in email client environments.

Sr.No Parameter & Description
1 to (Required). Specifies the receiver/receivers of the email
2 subject (Required). Defines the message to be sent. Each line should be separated with a LF (\n). Lines should not exceed 70 characters
3 message (Required). Defines the message to be sent. Each line should be separated with a LF (\n). Lines should not exceed 70 characters
4 headers (Optional). Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n)
5 parameters (Optional). Specifies an additional parameter to the send mail program.

A Simple Implementation Example

mail(“recipient@recipient.com”, “Message”, “A simple message.”, “From: The Sender <sender@sender.com>”);

Ways To Make Your PHP mail() Emails Less Spammy
A. Use Headers
In the simple example above, the from name and email address was added as the fourth parameter. Instead, consider using headers to set your From and Reply-To email addresses.

  $headers .= “Reply-To: The Sender <sender@sender.com>\r\n”;
$headers .= “Return-Path: The Sender <sender@sender.com>\r\n”;
$headers .= “From: The Sender <sender@sender.com>\r\n”;

But headers  are good for more than just setting details about the sender. They are also important for setting the content type, the email priority, and more. Here are how some additional headers look.

  $headers .= “Organization: Sender Organization\r\n”;
$headers .= “MIME-Version: 1.0\r\n”;
$headers .= “Content-type: text/plain; charset=iso-8859-1\r\n”;
$headers .= “X-Priority: 3\r\n”;
$headers .= “X-Mailer: PHP”. phpversion() .”\r\n”

Be sure to replace the fourth parameter with the $headers variable as shown below.

mail(“recipient@recipient.com”, “Message”, “A simple message.”, $headers);

B. The Message Sender Domain and Server Domain should Match
Spammers are notorious for sending emails from one server and trying to make the recipient believe that it came from somewhere else. So if you are sending an email from example@example.com, it is a good idea that the script reside on example.com.

C. Be sure to properly use the Content-type Attribute
The Content-type attribute enables a message sender to say whether or not an email is plain text  or html, or whether it has attachments. Obviously, the easiest to use content type is text/plain. You just add your text as shown in the simple example, and you are done. But when you use the other content types, additional pieces might be expected. For example, with the text/html content type, an html body tag is expected. Not having this tag could result in your email being marked as spam.

D. Verify that your Server is not Blacklisted
When a server is blacklisted, it means that the server has identified as one that has been sending a lot of spam. This results in recipient mail servers rejecting or filtering any mail that is received from that server.
So if your mail is not being received it is a good idea to verify that your server has not been blacklisted. This goes for both shared and dedicated servers. In a shared environment, it is common for other users on the server to be sending out spam. And in a dedicated environment, spammers may have found a way to exploit a vulnerability in a server or contact form to send out spam. So it is easy for either type of server to be blacklisted.

SHRI
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest
Inline Feedbacks
View all comments