<?php
// multiple recipients
$to = ‘text@examples.com'
// subject
$subject = 'HTML email';
// message
$message = '
<html>
<head>
<title>HTML EMAIL</title>
</head>
<body>
<p>Sample of HTML EMAIL!</p>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Joe <joes@examples.com>, Kelly <kellys@examplescom>' . "\r\n";
$headers .= 'From: HTML EMAIL <samples@examples.com>' . "\r\n";
$headers .= 'Cc: samples@examplescom' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
difference between mysql_connect() and mysql_pconnect()?
When mysql_pconnect() is used, the function initially tries to find an open persistent connection. If found; the identifier is returned. In this case, a new connection is not established. On the other hand, when mysql_connect() is used, a new connection is established.
Using mysql_pconnect(), the connection is not closed to enable future use.
Example:
mysql_pconnect("localhost","mysql_user","mysql_pwd");
Using mysql_pconnect(), the connection is not closed to enable future use.
Example:
mysql_pconnect("localhost","mysql_user","mysql_pwd");
what's difference between in_array() and array_search() in php?
in_array() checks if the specified value exits in the array,while array_search() searches an array for a given value and returns the key
example of in_array():
<?
$a1=array("a"=>"one","b"=>"two");
if(in_array("one",$a1))
{
echo "exit";
}else
{
echo "not exist";
}
//example of array_search():
$a1=array("a"=>"one","b"=>"two");
echo array_search("one",$a1);
it returns: a
?>
example of in_array():
<?
$a1=array("a"=>"one","b"=>"two");
if(in_array("one",$a1))
{
echo "exit";
}else
{
echo "not exist";
}
//example of array_search():
$a1=array("a"=>"one","b"=>"two");
echo array_search("one",$a1);
it returns: a
?>
array_slice() and array_splice() in php
array_slice() returns selected parts of array,while array_splice() removes and replaces specifield elements of an array
example of array_slice():
<?php
$a=array(0=>"one", 2=>"two",3=>"three", 4=>"four");
print_r(array_slice($a,1,2));
it returns: array([0]=>"two",[1]=>"three");
?>
example of array_splice():
<?php
$a=array(0=>"one", 2=>"two",3=>"three", 4=>"four");
$b=array(0=>"ones", 2=>"twos");
print_r(array_splice($a,0,2,$b));
it returns: array([0]=>"ones", [2]=>"twos",[3]=>"three",[ 4]=>"four");
?>
example of array_slice():
<?php
$a=array(0=>"one", 2=>"two",3=>"three", 4=>"four");
print_r(array_slice($a,1,2));
it returns: array([0]=>"two",[1]=>"three");
?>
example of array_splice():
<?php
$a=array(0=>"one", 2=>"two",3=>"three", 4=>"four");
$b=array(0=>"ones", 2=>"twos");
print_r(array_splice($a,0,2,$b));
it returns: array([0]=>"ones", [2]=>"twos",[3]=>"three",[ 4]=>"four");
?>
mysql_fetch_object and mysql_fetch_array in php
mysql_fetch_object returns the database as objects while mysql_fetch_array returns as an array. This will allow access to the data by the field names.
example of mysql_fetch_object
example of mysql_fetch_object
<?php
mysql_connect("hostname", "user", "password");
mysql_select_db("db");
$result = mysql_query("select * from content");
while ($res = mysql_fetch_object($result)) {
echo $res->username;
echo $res->place;
}
mysql_free_result($result);
?>
example of mysql_fetch_array
<?php
mysql_connect("hostname", "user", "password");
mysql_select_db("db");
$result = mysql_query("select * from content");
while ($res= mysql_fetch_array($result, MYSQL_ASSOC)) {
echo
$res[0];
}
?>
juggle in php?
Juggling means dealing with a variable type. In PHP a variables type is determined by the context in which it is used. If an integer value is assigned to a variable, it becomes an integer.
simple meaning urlencode in php
example of urlencode
<?php
echo '<a href="test?bar=', urlencode($userinput), '">';
?>
simple difference between md5(),crc32() and sha1() in PHP?
it main difference is the length of the hash generation. CRC32 returns 32 bit value, while sha1() returns a 128 bit value, and md5() returns a 160 bit value.
difference between htmlentities() and htmlspecialchars()
htmlspecialchars only takes care of <, >, single quote ‘, double quote " and ampersand.it does not takes all occurrence of html characters.but htmlentities translates all occurrences of character sequences that have different meaning in HTML.
main difference echo & print in php
main difference echo & print in php
echo is the most primitive of them, and just outputs the contents following the construct to the screen,it takes multiple parameter.Echo is faster when compared with print.
print is also a construct (so parentheses are optional when calling it),it does not take multiple parameters.
Echo does not have a return value, where as print returns a value indicating successful execution. Print returns 1 or 0 depending on the success
echo is the most primitive of them, and just outputs the contents following the construct to the screen,it takes multiple parameter.Echo is faster when compared with print.
print is also a construct (so parentheses are optional when calling it),it does not take multiple parameters.
Echo does not have a return value, where as print returns a value indicating successful execution. Print returns 1 or 0 depending on the success
difference between characters 23 and x23?
difference between characters 23 and x23
Characters 23 is octal 23,and x23 is hex 23.
Characters 23 is octal 23,and x23 is hex 23.
Simple meaning of PHP
Php is a web development language.Defenition of php(Hypertext Preprocessor),it is createted for web development,it embedded into html.
The father of php by Rasmus Lerdorf in 1995
Simple Meaning of php
Hypertext Preprocessor
server-side scripting language
Srcipt are executed on the server
Supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
Open source software
Free to download and use
The father of php by Rasmus Lerdorf in 1995
Simple Meaning of php
Hypertext Preprocessor
server-side scripting language
Srcipt are executed on the server
Supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
Open source software
Free to download and use
thumbnail generator in php with gd support
thumbnail generator in php with gd support
<?php
$imagefolder='.';
$thumbsfolder='.';
$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");
if ($pics[0]!="")
{
foreach ($pics as $p)
{
createthumb($p,"tn_".$p,150,150);
}
}
/*
Function ditchtn($arr,$thumbname)
filters out thumbnails
*/
function ditchtn($arr,$thumbname)
{
foreach ($arr as $item)
{
if (!preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}
}
return $tmparr;
}
/*
Function createthumb($name,$filename,$new_w,$new_h)
creates a resized image
variables:
$name Original filename
$filename Filename of the resized image
$new_w width of resized image
$new_h height of resized image
*/
function createthumb($name,$filename,$new_w,$new_h)
{
$system=explode(".",$name);
if (preg_match("/jpg
jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y)
{
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y)
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
/*
Function directory($directory,$filters)
reads the content of $directory, takes the files that apply to $filter
and returns an array of the filenames.
You can specify which files to read, for example
$files = directory(".","jpg,gif");
gets all jpg and gif files in this directory.
$files = directory(".","all");
gets all files.
*/
function directory($dir,$filters)
{
$handle=opendir($dir);
$files=array();
if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}
if ($filters != "all")
{
$filters=explode(",",$filters);
while (($file = readdir($handle))!==false)
{
for ($f=0;$f<sizeof($filters);$f++):
$system=explode(".",$file);
if ($system[1] == $filters[$f]){$files[] = $file;}
endfor;
}
}
closedir($handle);
return $files;
}
?>
<?php
$imagefolder='.';
$thumbsfolder='.';
$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");
if ($pics[0]!="")
{
foreach ($pics as $p)
{
createthumb($p,"tn_".$p,150,150);
}
}
/*
Function ditchtn($arr,$thumbname)
filters out thumbnails
*/
function ditchtn($arr,$thumbname)
{
foreach ($arr as $item)
{
if (!preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}
}
return $tmparr;
}
/*
Function createthumb($name,$filename,$new_w,$new_h)
creates a resized image
variables:
$name Original filename
$filename Filename of the resized image
$new_w width of resized image
$new_h height of resized image
*/
function createthumb($name,$filename,$new_w,$new_h)
{
$system=explode(".",$name);
if (preg_match("/jpg
jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y)
{
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y)
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
/*
Function directory($directory,$filters)
reads the content of $directory, takes the files that apply to $filter
and returns an array of the filenames.
You can specify which files to read, for example
$files = directory(".","jpg,gif");
gets all jpg and gif files in this directory.
$files = directory(".","all");
gets all files.
*/
function directory($dir,$filters)
{
$handle=opendir($dir);
$files=array();
if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}
if ($filters != "all")
{
$filters=explode(",",$filters);
while (($file = readdir($handle))!==false)
{
for ($f=0;$f<sizeof($filters);$f++):
$system=explode(".",$file);
if ($system[1] == $filters[$f]){$files[] = $file;}
endfor;
}
}
closedir($handle);
return $files;
}
?>
how can download file in php
how can download file in php
<?php
function file_download($file)
{
$dir = "../download/";
if ((isset($file))&&(file_exists($dir.$file))) {
header("Content-type: application/force-download");
header('Content-Disposition: inline; filename="' . $dir.$file . '"');
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($dir.$file));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $file . '"');
readfile("$dir$file");
} else {
echo "No file selected";
} //end if
}//end function
?>
<?php
function file_download($file)
{
$dir = "../download/";
if ((isset($file))&&(file_exists($dir.$file))) {
header("Content-type: application/force-download");
header('Content-Disposition: inline; filename="' . $dir.$file . '"');
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($dir.$file));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $file . '"');
readfile("$dir$file");
} else {
echo "No file selected";
} //end if
}//end function
?>
meaning of ereg_replace() and eregi_replace()?
meaning of ereg_replace() and eregi_replace()?
it replaces regular expression,eregi_replace() function is identical to ereg_replace() except that it ignores case distinction when matching alphabetic characters.
it replaces regular expression,eregi_replace() function is identical to ereg_replace() except that it ignores case distinction when matching alphabetic characters.
simple word strstr and stristr?
Strstr finds first occurrence of a string inside another string,stristr() is idential to strstr() except that it is case insensitive.
strstr() returns part of a given string from the first occurrence of a given substring to the end of the string. For example: strstr("user@example.com","@") will return "@example.com".
stristr() is idential to strstr() except that it is case insensitive.
strstr() returns part of a given string from the first occurrence of a given substring to the end of the string. For example: strstr("user@example.com","@") will return "@example.com".
stristr() is idential to strstr() except that it is case insensitive.
simple meaning of explode and implode in php
Explode split a string by string,it change a string into array,implode means return a string from the elements of an array.
example of explode.
example of impode.
example of explode.
<?php
$text= "one two three";
$res= explode(" ", $
text
);
echo $res[0]; // one
echo $res[1]; //two
?>
example of impode.
<?php
$test= array('one', 'two', 'three');
$res = implode(",", $test);
echo $
res
; // one,two,three
?>
simple php form Hijacking
Few more coding practices can be done to avoid PP Form HijackingUser Input Sanitization-Never trust web user submitted data. Follow good clieint side data validation practices with regular expressions before submitting data to the server
how can Prevent form hijacking in PHP?
(1). Make register_globals to off to prevent Form Injection with malicious data.
(2). Make Error_reporting to E_ALL so that all variables will be intialized before using them.
(3). Make practice of using htmlentities(), strip_tags(), utf8_decode() and addslashes() for filtering malicious data in php
(4). Make practice of using mysql_escape_string() in mysql.
how can Prevent form hijacking in PHP?
(1). Make register_globals to off to prevent Form Injection with malicious data.
(2). Make Error_reporting to E_ALL so that all variables will be intialized before using them.
(3). Make practice of using htmlentities(), strip_tags(), utf8_decode() and addslashes() for filtering malicious data in php
(4). Make practice of using mysql_escape_string() in mysql.
simple meaning of unset in php
Unset is used for destroys the specified variables.
example:
<?php
function destroy()
{
global $test;
unset($test);
}
$test = 'hai';
destroy();
echo $test ;
?>
it will get output :hai
example:
<?php
function destroy()
{
global $test;
unset($test);
}
$test = 'hai';
destroy();
echo $test ;
?>
it will get output :hai
simple meaning of unlink in php
unlink() is a function used for file handling in php,
it will delete the file content.
example:
<?php
$files = "image/log.jpeg";
if (!unlink($files))
{
echo ("Error deleting $files");
}
else
{
echo ("Deleted $files");
}
?>
it will delete the file content.
example:
<?php
$files = "image/log.jpeg";
if (!unlink($files))
{
echo ("Error deleting $files");
}
else
{
echo ("Deleted $files");
}
?>
submit without a submit button in php
submit without a submit button in php
If you don’t want to use the Submit button to submit a form, you can some JavaScript code.
For example:
javascript: document.myform.submit();
If you don’t want to use the Submit button to submit a form, you can some JavaScript code.
For example:
javascript: document.myform.submit();
simple meaning of Md5 in php
Md5 is hash generator of a string,it is used for encryption in php,it returns 32 character hex numbe.MD5 "compresses" any stream of bytes into a 128 bit value.
Syntax in Md5
<?php
md5(string,raw);
example
<?php
$test = "Hello";
echo md5($str);
?>
it will be above output:
Syntax in Md5
<?php
md5(string,raw);
example
<?php
$test = "Hello";
echo md5($str);
?>
it will be above output:
8b1a9953c4611296a827abf8c47804d7 |
simple urlencode and urldecode in php
urlencode()
it returns the URL encoded version of the given string. URL coding converts special characters into % signs followed by two hex digits. For example: urlencode(”10.00%”) will return “10%2E00%25″.
URL encoded strings are safe to be used as part of URLs.
urldecode()
it returns the URL decoded version of the given string.Anwser 2:
string urlencode(str) – Returns the URL encoded version of the input string. String values to be used in URL query string need to be URL encoded. In the URL encoded version:
Alphanumeric characters are maintained as is.
Space characters are converted to “+” characters.
Other non-alphanumeric characters are converted “%” followed by two hex digits representing the converted character.
string urldecode(str) – Returns the original string of the input URL encoded string.For example:
$discount =”20.00%”;
$url = “http://domains.com/submits.php?discs=”.urlencode($discount);
echo $url;You will get “http://domains.com/submits.php?discs=20%2E00%25″
it returns the URL encoded version of the given string. URL coding converts special characters into % signs followed by two hex digits. For example: urlencode(”10.00%”) will return “10%2E00%25″.
URL encoded strings are safe to be used as part of URLs.
urldecode()
it returns the URL decoded version of the given string.Anwser 2:
string urlencode(str) – Returns the URL encoded version of the input string. String values to be used in URL query string need to be URL encoded. In the URL encoded version:
Alphanumeric characters are maintained as is.
Space characters are converted to “+” characters.
Other non-alphanumeric characters are converted “%” followed by two hex digits representing the converted character.
string urldecode(str) – Returns the original string of the input URL encoded string.For example:
$discount =”20.00%”;
$url = “http://domains.com/submits.php?discs=”.urlencode($discount);
echo $url;You will get “http://domains.com/submits.php?discs=20%2E00%25″
Subscribe to:
Posts (Atom)
it is function using when encoding a string to be used in a query part of a url, as a convenient way to pass variables to the next page.