Thursday, August 9, 2007

PHP - get client ip address

Function can get client ip address using this function :)

//-----------------------------------------------

function getIP() {
$ip;
if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else
$ip = "UNKNOWN";
return $ip;


}

//-----------------------------------------------

Usage:
<

$client_ip=getIP();
echo "Your IP :".$client_ip;
?>

//--------------------------------------------


4 comments:

Anonymous said...

Thanks very useful website.. im expecting more..!

iamRandy said...

Thanks! This was exactly what I needed! Well done.

ilove said...

i try this scrip on my server. it not work in web proxy

this web proxy
http://www.japanweb.info/

Can i block IP client come from web proxy ?

i can't block IP from every web proxy.

sorry for my bad English.

Anonymous said...

the below code will work well and fine..

$ip= $REMOTE_ADDR;
echo "
Your ip : " .GetHostByName($ip);

thank u...