ISSUE :
 
Profile photo
Submitted by Bill Tucker
on June 20th, 2011 at 21:47:50 EST

How to get client IP address using PHP

I need to capture client user IP address using PHP. I need this specially for my logging and authentication purpose. How do I get client user IP address?


Profile photo
Replied by SBajra
on June 20th, 2011 at 22:06:43 EST
You can user Server Variables to get the IP address.
Try this code to get IP address in PHP:
function GetIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
Accepted Answer
Accepted
 Answer

Profile photo
Replied by Bill Tucker
on June 21st, 2011 at 18:31:38 EST
Thanks. This is exactly what I am looking for. It works great.