Skip to content
</> Mücahit TopalA developer's notebook

Blog/PHP & MySQL

Getting the Real User IP with PHP

Mücahit Topal 1 min read 4,039 views TREN

Function one;

function GetIP(){
	if(getenv("HTTP_CLIENT_IP")) {
 		$ip = getenv("HTTP_CLIENT_IP");
 	} elseif(getenv("HTTP_X_FORWARDED_FOR")) {
 		$ip = getenv("HTTP_X_FORWARDED_FOR");
 		if (strstr($ip, ',')) {
 			$tmp = explode (',', $ip);
 			$ip = trim($tmp[0]);
 		}
 	} else {
 	$ip = getenv("REMOTE_ADDR");
 	}
	return $ip;
}
echo "Real IP = ".GetIP();

 

Function two;

function GetIP()  
{  
    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;  
}  

echo "Real IP: ".GetIP();

 

Function three;


function GetInfo() { 
   $proxy=""; 
   $IP = ""; 
   if (isSet($_SERVER)) { 
       if (isSet($_SERVER["HTTP_X_FORWARDED_FOR"])) { 
           $IP = $_SERVER["HTTP_X_FORWARDED_FOR"]; 
           $proxy  = $_SERVER["REMOTE_ADDR"]; 
       } elseif (isSet($_SERVER["HTTP_CLIENT_IP"])) { 
           $IP = $_SERVER["HTTP_CLIENT_IP"]; 
       } else { 
           $IP = $_SERVER["REMOTE_ADDR"]; 
       } 
   } else { 
       if ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) { 
           $IP = getenv( 'HTTP_X_FORWARDED_FOR' ); 
           $proxy = getenv( 'REMOTE_ADDR' ); 
       } elseif ( getenv( 'HTTP_CLIENT_IP' ) ) { 
           $IP = getenv( 'HTTP_CLIENT_IP' ); 
       } else { 
           $IP = getenv( 'REMOTE_ADDR' ); 
       } 
   } 
   if (strstr($IP, ',')) { 
       $ips = explode(',', $IP); 
       $IP = $ips[0]; 
   } 
   $RemoteInfo['ip']=$IP; 
   $RemoteInfo['hostadi']=@GetHostByAddr($IP); 
   $RemoteInfo['proxy']=$proxy; 
   return $RemoteInfo; 
} 




#let's assign the user's information returned by the function to a variable  
$user=GetInfo();  

#let's print the user's ip address to the screen  
echo $user['ip']."
";  

#let's print the user's host name to the screen  
echo $user['hostadi']."
";  

#for the user's proxy, if there is one  
echo $user['proxy']."
";  

 

,,,

Share X Facebook LinkedIn WhatsApp

More from this category

Comments 0

No comments yet.

Leave a comment

Comments appear after approval.