Friday 4 March 2011

Automatically refresh the web page every 5 seconds, with PHP

Redict the web page with PHP

you can easily redirect to the different web page using simple php script.By using PHP header() function you can easily redirect to new page without having new extra clicks.

the following PHP script redirect the user to bugphp.com











1<?php










2header( 'Location: http://www.bugphp.com/' ) ;










3?>




make sure,it will not work if you sent any data to the browser before this function.if you write echo or print or any HTML statements or functions before the redirection you will get an error,to avoid this use PHP output buffering as follows











1<?php










2ob_start();










3echo 'header test';










4header( 'Location: http://www.bugphp.com/' ) ;










5ob_flush();










6?>




Automatically refresh the web page every 5 seconds, with PHP









1<?php










2$url=$_SERVER['REQUEST_URI'];










3header("Refresh: 5; URL=\"" . $url . "\""); // redirect in 5 seconds









4?>

how to make Alphabet Navigation Menu with PHP?

In this post we are going to discuses about alphabet navigation.before that do you know generating alphabets in a loop.if don’t know look at below example once










1<?php










2










3for ($i=65; $i<=90; $i++) {










4echo chr($i);// chr returns specific character










5//out put :ABCDEFGHIJKLMNOPQRSTUVWXYZ










6}










7?>




In the above example chr() function returns specific character & chr() also accepts negative numbers as an ascii code[char(-100)]

You can make above output with range() function,look the example :














1<?php foreach(range('A','Z') as $i) {










2echo $i; }










3?>




output:ABCDEFGHIJKLMNOPQRSTUVWXYZ

now you can easily understand below alphabet navigation menu














1<?php










2$LetterList = range('A','Z');










3foreach ($LetterList as $litter) {










4echo '<a href="http://example.com/letter.php?letter=' . $litter. '">' .$litter. '</a>'.' ';










5}










6?>




Outpup : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

you can make above output in another way














1<?php










2$LetterList = array_merge(array('0-9'),range('A','Z'));










3foreach ($LetterList as $value) {










4echo '<a href="http://example.com/letter.php?letter=' . $value . '">' .$value . '</a>'.' ';










5}










6?>




out put:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

The description of the functions used in this tutorial are as follows

chr() this function return a spesific character

range() this function crate an array containing range of elements

foreach()foreach is used to loop over all elements of an array

array_merge() this function merges one or more arrays

How to count how many times a certain word is used ,using php?













01<?php










02$count = 0; // count start form zero










03$string = "bugphp -free online tutorials bugphp.com bugphp bugphp.com bugphpbugphp bugphp";










04










05foreach( str_word_count($string ,1) as $a ) {










06 if( strtolower($a) == strtolower('bugphp') )  {










07 $count++;










08 }










09}










10echo "$count";









11?>

How to get absolute path on a web server using PHP ?

We can find absolute path in different ways on a web server using php, i am showing some of that practices here










01<?php










02echo realpath(dirname(__FILE__));










03// realpath() , This function removes all symbolic path and returns the absolute path name , this function return >false an failer










04//dirname() , Returns the parent directory path










05// __FILE__ ,The full path and file name of the file










06?>










07<?php










08echo getcwd();










09//getcwd(), function get the current working directory , returns true on success return >false on failure










10?>










11<?php










12print_r($_SERVER['DOCUMENT_ROOT']);










13//$_SERVER is an array with headers,path,script location information.










14//$_SERVER['DOCUMENT_ROOT'] , returns the current  executing script directory










15?>










16<?php










17// absolute path with file name










18echo $_SERVER['SCRIPT_FILENAME'];










19?>




The description of the functions used in this tutorial are as follows
realpath() , This function removes all symbolic path and returns the absolute path name , this function return false an failure
dirname() , Returns the parent directory path
getcwd(), function get the current working directory , returns true on success return flase on failure
$_SERVER is an array with headers,path,script location information.

Getting visitor IP address with PHP

We can easily get IP Address of visitor with php environment variables .User IP Address was stored in following php Environment variables $_SERVER['REMOTE_ADDR'],$_SERVER['HTTP_CLIENT_IP'] and $_SERVER['HTTP_X_FORWARDED_FOR'].

most common practice for to get IP address of visitor with php is













1<?php










2$ip=$_SERVER['REMOTE_ADDR'];










3echo 'User ip Address'.$ip;










4?>




We can’t trust above script generated ip address if user use proxy sever it simple return proxy IP.So to get user’s real IP address we should check for proxy and shared servers first.i have wrote one php function to get real IP address of visitor.











01<?php










02function GetUserIp() {










03if(isset($_SERVER['HTTP_CLIENT_IP'])) // check for shared servers










04{










05$ip = $_SERVER['HTTP_CLIENT_IP'];










06}










07elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) // check for proxy servers










08{










09$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];










10} else {










11$ip = $_SERVER['REMOTE_ADDR']; // general method










12}










13list($ip) = explode(',',$ip);










14return $ip;










15}










16echo GetUserIp(); // print ip address









17?>