Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

Get Visitors Real IP Address in PHP

Hello Friends,

If you want track visitor's or user's IP address than you may use $_SERVER['REMOTE_ADDR']. Well, you might be shocked to know that it may not return the real IP address of the visitor/user at all time. Here are the cases when it happens. If visitor/user is connected to the Internet through Proxy Server then $_SERVER['REMOTE_ADDR'] in PHP will give the IP address of the proxy server and it will not be visitor's/user's IP address.

In PHP , there are other server variables ( HTTP_CLIENT_IP , HTTP_X_FORWARDED_FOR along with REMOTE_ADDR) using which we can find the real IP address even if visitor/user is connected to Proxy Server. Here is a function in PHP to find the real IP address of the visitor's/user's PC.

function get_real_IP_address()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}

In above PHP function, it wil first check for IP address of visitor's/user's PC and return if it get IP address. If client's IP address is not available then it will find for forwarded for IP address using HTTP_X_FORWARDED_FOR. If still it did not get IP address than it return IP address using REMOTE_ADDR.

To know more about programming,JavaScript issues,jQuery,Expression Engine,MYSQL database and Open-source, enter your email address below. We will send you free tutorials.

Enter your email address:
 
Feel free to ask any question. Just leave your comment below and we will answer your comment with in 24 hours.
Share:

6 Comments

Leave a Reply

Your email address will not be published.