php question
#1

hello all boys/girls, please do anyone know how to get Visitor's IP Address on PHP ??
i tried
PHP код:
function GetClientIP($validate False)
{
  
$ipkeys = array(
  
'REMOTE_ADDR'
  
'HTTP_CLIENT_IP'
  
'HTTP_X_FORWARDED_FOR'
  
'HTTP_X_FORWARDED'
  
'HTTP_FORWARDED_FOR'
  
'HTTP_FORWARDED'
  
'HTTP_X_CLUSTER_CLIENT_IP'
  
);
  
$ip = array();
  foreach(
$ipkeys as $keyword){
    if( isset(
$_SERVER[$keyword]) ){
      if(
$validate){
        if(
ValidatePublicIP($_SERVER[$keyword]) ){
          
$ip[] = $_SERVER[$keyword];
        }
      }else{
        
$ip[] = $_SERVER[$keyword];
      }
    }
  }
  
$ip = ( empty($ip) ? 'Unknown' implode(", "$ip) );
  return 
$ip;
}
function 
ValidatePublicIP($ip)
{
  if(
filter_var($ipFILTER_VALIDATE_IPFILTER_FLAG_NO_PRIV_RANGE FILTER_FLAG_NO_RES_RANGE)) {
    return 
true;
  }
  else {
    return 
false;
  }

but it always returns Unknown or ::1

please some help
Reply
#2

::1 is basically 127.0.0.1, so it's returning correct.
Reply
#3

Quote:
Originally Posted by Meller
Посмотреть сообщение
::1 is basically 127.0.0.1, so it's returning correct.
i was testing it on XAMPP with internet connection but will try testing it on my website...
Reply
#4

::1 is an IPv6 IP address, what you probably want is IPv4
Reply
#5

PHP код:
function get_client_ip() {
        
$ipaddress '';
        if (isset(
$_SERVER['HTTP_CLIENT_IP']))
            
$ipaddress $_SERVER['HTTP_CLIENT_IP'];
        else if(isset(
$_SERVER['HTTP_X_FORWARDED_FOR']))
            
$ipaddress $_SERVER['HTTP_X_FORWARDED_FOR'];
        else if(isset(
$_SERVER['HTTP_X_FORWARDED']))
            
$ipaddress $_SERVER['HTTP_X_FORWARDED'];
        else if(isset(
$_SERVER['HTTP_FORWARDED_FOR']))
            
$ipaddress $_SERVER['HTTP_FORWARDED_FOR'];
        else if(isset(
$_SERVER['HTTP_FORWARDED']))
            
$ipaddress $_SERVER['HTTP_FORWARDED'];
        else if(isset(
$_SERVER['REMOTE_ADDR']))
            
$ipaddress $_SERVER['REMOTE_ADDR'];
        else
            
$ipaddress 'UNKNOWN';
        return 
$ipaddress;
    }
    
$clientIP get_client_ip(); 
Reply
#6

thanks all, after testing it on web it worked 100%
+repped
Reply


Forum Jump:


Users browsing this thread: 7 Guest(s)