Today, i want to share you some useful PHP code snippets which may reduce your development time.
1.Send HTML Mail
[php]
<?php
$to = "[email protected]";
$subject = "This is my test html email send using php mail function";
$body = "Body of your message here you can use HTML too. <br /> <h1> Header </h1> ;";
$headers = "From: User \r\n";
$headers.= "Reply-To: [email protected]\r\n";
$headers.= "Return-Path: [email protected]\r\n";
$headers.= <"X-Mailer: PHP5\n";
$headers.= ‘MIME-Version: 1.0’."\n";
$headers.= ‘Content-type: text/html; charset=iso-8859-1’."\r\n";
mail($to,$subject,$body,$headers);
?>
[/php]
2.Detect Browser using PHP function
[php]
<?php
echo $_SERVER[‘HTTP_USER_AGENT’] . "\n\n";
$browser = get_browser(null, true);
print_r($browser);
// sample output
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3
Array
(
[browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
[browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*
[parent] => Firefox 0.9
[platform] => WinXP
[browser] => Firefox
[version] => 0.9
[majorver] => 0
[minorver] => 9
[cssversion] => 2
[frames] => 1
[iframes] => 1
[tables] => 1
[cookies] => 1
[backgroundsounds] =>
[vbscript] =>
[javascript] => 1
[javaapplets] => 1
[activexcontrols] =>
[cdf] =>
[aol] =>
[beta] => 1
[win16] =>
[crawler] =>
[stripper] =>
[wap] =>
[netclr] =>
)
?>
[/php]
3. Get Remote IP address
[php]
echo $_SERVER[‘REMOTE_ADDR’];
//sample output 127.0.0.1
[/php]
4. Read CSV file with PHP
[php]
<?php
function getCSVFile ( $CSVfile, $delimiter ) {
if(isset($CSVfile) AND isset($delimiter) ){
//Open csv file for reading.
$handleCSV = fopen($CSVfile, "r");
if($handleCSV) {
//Read each line and print the line out.
while (($line_array = fgetcsv($handleCSV, 4000, $delimiter)) !== false) {
//Do something to each line.
echo "<pre>";
print_r($line_array);
echo "</pre>";
}
fclose($handleCSV);
}
}
}
//url of the csv file
$CSVfile = ‘test.csv’;
//The delimiter that is used in csv file.
$delimiter = ‘,’;
getCSVFile ($CSVfile, $delimiter );
?>
[/php]
5. Check if server is HTTPS or not
[php]
if ($_SERVER[‘HTTPS’] != "on") {
echo "This is not HTTPS";
}else{
echo "This is HTTPS";
}
[/php]
6. Get short urls for Twitter
[php]
function getTinyUrl($url) {
return file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
}
[/php]
7. Simple PHP Image Crop
[php]
$file = $_FILES[‘image’][‘name’];
$newwidth = 200; // SET YOUR DESIRED WIDTH
$small_image = str_replace(" ", "_", $file);
$small_image = "small_".$small_image;
$smallfile = $_FILES[‘image’][‘tmp_name’];
$src = imagecreatefromjpeg($smallfile);
list($width,$height)=getimagesize($smallfile);
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = $imagepath."small/".$small_image;
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
[/php]
8. Simple PHP Redirect
[php]
function redirect($url,$die=true,$permanent=false) {
if(headers_sent()) {
print("<script type=\"text/javascript\">window.location.href='{$url}’;</script>");
} else {
if($permanent) {
header("HTTP/1.1 301 Moved Permanently");
}
header("Location: {$url}");
}
if($die) { die(); }
}
[/php]
Writer
Dynamic Web Lab Editorial
We share how we design, engineer, and scale digital products across the GCC, Europe, and the US.
Tags
Need help implementing this?
We turn these playbooks into shipped features. Let us scope your roadmap and support your team.
Start a project conversation