Tuesday 14 August 2018

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;
}

0 comments:

Post a Comment