Saturday 23 April 2011

PHP Header Script With Examples





HEADERS

*Header is a small amount of data packet network protocols to transform the information from one location to another location. You can transform the information with the header function in PHP.

danphp1.php

<?php
echo "welcome";
?>
danphp2.php

<?php
if(isset($_POST['sub']))
{
header("location:page1.php");
}
?>
<form method="post" action="">
<input type="submit" name="sub" value="Go">
</form>

danphp3.php

<?php
if(isset($_POST['sub']))
{
header("location:page1.php");
}
else if(isset($_POST['sub1']))
{
header ("location:page2.php");
}
?>
<form method="post" action="">
<input type="submit" name="sub" value="page1">
<input type="submit" name="sub1" value="page2">
</form>

Execute above files and observe the header function and works.

File Handling Concepts in PHP





File Handling Concepts in PHP


*By using this concept we can read the contents of a file. We can also write the contents in a file.
*To operate a file, first we need to open the file using “file mode”.

R(read) : To read the file contents. If you open the file using read mode the file pointer locates at the beginning of the file.

W(write) : If you open the file with write mode, we can write the content in file. First it removes the previous content of the file and places the file pointer at the beginning of the file. If the file is not available it creates a new file.

A(append) : To append the new content to the existing file. The file pointer locates at the end of the file. If the file is not available it creates a new file.

RT(read,write) : To read and write the contents of a file. It will not create the new file, if the file is not available. It doesn’t remove the previous data.

WT : It Is same as write mode, we can also read and write the file contents. It will create a new file, if the file is not exists. It removes the previous data.

AT: By using this mode we can read and also we can append the file contents.

Fopen: By using this mode we can open the files.

Fread: To read the contents of a file. It contents two arguments 1.file pointer 2. Size in bytes.

Fwrite: To write the contents in a file. The arguments are file pointers and new content.>It over writes the previous content.

Filesize: It returns the total number of bytes occupied by the file.


Practice Scripts on Above Concepts
1. Sample Practice Scripts
<?php
$fp=fopen("abc.txt","r");
$size=filesize("danphp.txt");
$cont=fread($fp,$size);
echo $cont;
?>
* Save one text file in htdocs as as “danphp.txt”, you can see the display of the document text on the browser.
2. Sample Practice Scripts



<?php
$fp=fopen("abc.txt","w");
echo fwrite($fp, " New Content");
?>
*If the file is not exits it will create a new file.

3. Sample Practice Scripts

<?php
$fp=fopen("abc.txt","a");
echo fwrite($fp,"New Cont");
?>

rewind:  By using this function we can locate the file pointers at the beginning of the file.


<?php
$fp=fopen("abc.txt","rt");
fwrite($fp,"smith");
rewind($fp);
echo fread($fp,filesize("abc.txt"));
?>

fseek: By using this function we can locate the file pointers in the specified location.


<?php
$fp=fopen("abc.txt","wt");
fwrite($fp,"PHP is SSS");
fseek($fp,2);
echo fread($fp,filesize("abc.txt"));
?>

fgets: This function is used to read a line from a file and locates the pointers at the beginning of the next line.

<?php
$fp=fopen("abc.txt","r");
$lc=0;
while ($line=fgets($fp))
{
$lc++;
echo $line;
}
echo "<br> Total Lines Are $lc";
?>

fgetss: It is same as “fgets”, but it ignores the HTML elements.

fgetc: By using this function, we can read a character from a file.

file_get_contents: By using this function we can get the all contents of a file, without file 
pointers.

file_put_contents: By using this function we can put some content in a file without file pointers.


<?php
echo file_put_contents("abc.txt", "scott");
?>
readfile: By using this function, we can read the contents of a file and at the same time we can write the file contents on the buffer.

filectime: This function returns the created date nad time information of a file in time stand format.
Time stand format-: total number of seconds from Jan 1st 1970.

filemtime: This function returns the modified date and time information of a file.

fileatime: It returns the last access date and time information of a file.

copy: This function copies the specified file into the target location.

file_exists: By using this function we can check whether the input file is existed or not.

unset: By using this function we can delete the file permanently from hard disk

1.Dan PHP Sample Script
<?php
readfile("abc.txt");
?>
2. Dan PHP Sample Script

<?php
$arr=file(“abc.txt”);
print_r($arr);
?>


mrdir: This function is used to create the new directory.

rmdir: This function is used to remove the directory.

opendir: This function is used to open a directory.

readdir: This function is used to read the content of a directory.

closedir: This function is used to close directory.

scandir: This function reads the all files of a directory and returns as an array.

Dan php Sample Script:


<?php
echo getcwd();
?>

Dan php Sample Script 2 

<?php
$dh=opendir("c:/xampp");
while ($file=readdir($dh))
{
echo $file;
echo "<br>";
}
?>


Dan php Sample Script 3



<?php
$dh=opendir("c:/xampp");
while($file=readdir($dh));
{
$text=strchr($file,".");
if($text==".exe");
{
echo $file;
echo "<br>";
}
}
?>


arry: Array is a collection of hetero genius elements. PHP is loosely typed language, that is the reason why we can store any type of elements in a array. In php array is construct that is why we can store the elements using declaration.

Dan Php Sampe Script 2 

<?php
$arr=array(10,100=>'php',30,0=>40);
print_r($arr);
?>

Dan Php Sampe Script 2 


<?php
$arr=array('Ap'=>array("hyd","DEL"),"TN"=>array('chennai','kanchi'));
print_r($arr);
echo $arr['Ap'][0];
?>

foreach: By using this function we can read the all elements of an array without initialization condition and increment.

Dan Php Sampe Script


<?php
$arr=array(10,'a'=>20,30);
foreach($arr as $ke=>$va)
{
echo "key is $ke value is $va";
echo "<br>";
}
?>

count: By using this function we can display the total number of array elements.

array_push: This function is used to add an element at the end of arry and returns the total number of elements.


Dan Php Sampe Script
<?php
$arr=array(10,20,30);
echo array_push($arr,40);
print_r($arr);
?>

array_pop: This function deletes the last elements of an array and returns the value of that element.


Dan Php Sampe Script

<?php
$arr=array(10,20,30);
echo array_pop($arr);
print_r($arr);
?>

array_shift: This function deletes the fist element of an array and retunes the value of the elements.

Dan Php Sampe Script


<?php
$arr=array(10,20,30);
echo array_shift($arr);
print_r($arr);
?>

array_unshift: By using this function we can add the elements at the beginning of an array and it displays the total number of elements.

Dan Php Sampe Script



<?php
$arr=array(10,20,30);
echo array_unshift($arr,5);
print_r($arr);
?>

sort: This. Function erases the array elements in the ascending order with new keys.

Dan Php Sampe Script


<?php
$arr=array(40,'a'=>10,20,30);
sort($arr);
print_r($arr);
?>

rsort: This function arranges the array elements in descending order.

assort: This function arranges the array elements in ascending order with the original keys.

ksort: This function arranges the array elements in ascending order base on the keys.

Dan Php Sampe Script

<?php

$arr=array(1=>40,3=>10,2=>20,0=>30);
ksort($arr);
print_r($arr);
?>

krsort: Based on the key, this function arranges the array elements in descending order.

array_key: It returns the all keys of an array as new array.

array_values: It returns the all values of an array as new array.

Dan Php Sampe Script

<?php
$arr=array('a'=>40,3=>10,2=>20,0=>30);
print_r(array_keys($arr));
?>


explode: This function divides the string as array elements based on the input elements.

Dan Php Sampe Script

<?php
$str="Welcome to Dan PHP Site";
$arr=explode("",$str);
print_r($arr);
?>

implode: By using this function we can join the array elements as string.

Dan Php Sampe Script


<?php
$arr=array("dan","PHP");
echo implode($arr,"/");
?>

extract: This function divides the array elements as a variables. This function will work with
only associative array elements.


Dan Php Sampe Script
<?php
$arr=array('a'=>10,'b'=>20);
extract($arr);
echo $a;
echo $b;
?>

array_chunk: This function splits an array into chunks of array.

Dan Php Sampe Script

<?php
$arr=array(10,20,30,40,50);
print_r(array_chunk($arr,3));
?>



array_combine: It combines the elements of two arrays, 1.it returns the new array keys are the
values of the first array 2.new array values are the values of second array.

Dan Php Sampe Script


<?php
$arr=array('a','b','c');
$arr1=array(10,20,30);
print_r(array_combine($arr,$arr1));
?>

array_count_values : This function returns an array with number of occurrence for each value.

Dan Php Sampe Script


<?php
$arr=array(10,20,30,10);
print_r(arraY_count_values($arr));
?>

array_diff: It compares the two array elements and returns the differences. 

Dan Php Sampe Script

<?php
$arr=array(10,20,30,);
$arr1=array(10,40,20);
print_r(array_diff($arr,$arr1));
?>

array_diff_assoc: This function compares the both array keys and values and returns the
differences.


Dan Php Sampe Script


<?php
$arr=array(10,20,30,);
$arr1=array(10,40,20);
print_r(array_diff_assoc($arr,$arr1));
?>
array_diff_key: Compares the keys of 2 arrays and returns the differences.

Dan Php Sampe Script


<?php
$arr=array(10,'a'=>20,30,);
$arr1=array(10,40,20);
print_r(array_diff_key($arr,$arr1));
?>

array_sum: It returns the sum of all elements of an array.

Dan Php Sampe Script



<?php
$arr=array(10,'a'=>20,30,);
echo array_sum($arr);
?>

array_product: It is a function and it returns the product of all elements of an array.

Dan Php Sampe Script

<?php
$arr=array(10,'a'=>20);
echo array_product($arr);
?>

shuffle : This function shuffles the elements of an array.

Dan Php Sampe Script


<?php
$arr=array(10,'a'=>20,30,'a');
shuffle($arr);
print_r($arr);
?>

array_merge: It merges two or more than two array as new arrays.


Dan Php Sampe Script



<?php
$arr=array(10,'a'=>20,30,);
$arr1=array('a','b','c');
print_r(array_merge($arr,$arr1));
?>

array_unique: This function removes the duplicate elements and returns the unique values of an array.


join: It is same as implode used to join the elements of an array. Returns as string. 


Dan Php Sampe Script



<?php
$arr=array(10,20,30,40);
print_r(array_unique($arr));
?>

array_key_exists: This function checks the specified key existed in the array or not.


Dan Php Sampe Script

<?php
$arr=array(10,20,30,40);
echo array_key_exists(1,$arr);
?>
array_search: This function searches an array for a given value and returns the key.

Dan Php Sampe Script


<?php
$arr=array(10,20,30,40);
echo array_search(20,$arr);
?>

array_reverse: This function displays the elements of an array in reverse direction.

Dan Php Sampe Script


<?php
$arr=array(10,20,30,10);
print_r(array_reverse($arr);
?>



array_slice: This function returns selected parts of an array in reverse direction.


Dan Php Sampe Script



<?php
$arr=array(10,20,30,40);
print_r(array_slice($arr,2));
?>

Configuration directives to work with the Sessions



Configuration directives to work with the Sessions


*session.auto_start: This directive is used to start the sessions when the request is started. The default value is 0.

*session.save_path: This directive is used to provide the path to save the sessions.

*session.save_handler: This directive is used to provide the location, when you want to save store the sessions in client system.

session.name: This directive is used to provide the name to store the sessions in client system. The default value is pgpsessid

session.use_cookies: This directive is used to, whether use cookies or not to store the sessions. The default value is 1.

session.cookie_lifetime: By using this directive we can change the lifetime of cookie to store the sessions. The default value is 0.

session.gc_maxlifetime : By using this directive we can provide the maximum lifetime to destroy the data of session files with the help of garbage collector.

*garbage collector: garbage collector is a tool used to check the unreferenced objects. The default value is 1440(24 minutes).

$_SERVER: By using this super global variable we can get the information of web server. Like software, server ip address, remote user ip address, server port numbers.

$_SERVER.[SERVER_SOFTWARE] : This element returns the complete information of web server.

$_SERVER[SERVER_NAME]:  By using this element we can get the name of the web server.

$_SERVER[SERVER_ADDR] : By using this element, we can get the ip address of web server.

$_SERVER[SERVER_PORT]: This element holds the port number of web server.

$_SERVER[REMOTE_ADDR] : By using this element we can find out the visitors ip address.

$_SERVER[SERVER_ADMIN] : This element holds the admin name along with the server name.
$_SERVER[SCRIPT_FILENAME]: This element holds the complete path of the executing file.

$_SERVER[REMOTE_PORT] : This element holds the port number of client system.

$_SERVER[SERVER_PROTOCAL] : This elect holds the information of protocol, what
we are using to transform the information.

$_SERVER[QUERY_STRING] : This element holds the information of a query string.

$_SERVER[REQUEST_URI] : This element holds the current file name along with the query string.

$_SERVER[SCRIPT_NAME] & $_SERVER[PHP-SELF] : By using these elements we can get the current script name.

$_ENV : By using this super global variable we can get the environment variable values.

*phpinfo : By using this function we can get the all php.ini configurations directives.

*phpinfo(16) : Returns the all environment variables.