How to get ip address from php


Are you using $_SERVER[‘REMOTE_ADDR’] to find the the client’s IP address in PHP? Well dude, you might be amazed to know that it may not return the true IP address of the client at all time. If your client is connected to the Internet through Proxy Server then $_SERVER[‘REMOTE_ADDR’] in PHP just returns the the IP address of the proxy server not of the client’s machine. So here is a simple function in PHP to find the real IP address of the client’s machine. There are extra Server variable which might be available to determine the exact IP address of the client’s machine in PHP, they are HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR.

PHP-Code:

function  geIPAddress()
{

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

 

If you have any query then feel free to contact me at Jainish Senjaliya

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s