Home  >  Article  >  Backend Development  >  php gets the real ip of the client

php gets the real ip of the client

巴扎黑
巴扎黑Original
2016-11-23 13:50:40864browse

function GetIP(){ 
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) 
$ip = getenv("HTTP_CLIENT_IP"); 
else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) 
$ip = getenv("HTTP_X_FORWARDED_FOR"); 
else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) 
$ip = getenv("REMOTE_ADDR"); 
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) 
$ip = $_SERVER['REMOTE_ADDR']; 
else 
$ip = "unknown"; 
return($ip); 
}

regist=off problem
if ($register_globals!=1) {
@extract($_SERVER, EXTR_SKIP);
@extract($_COOKIE, EXTR_SKIP);
@extract($_SESSION, EXTR_SKIP);
@extract ($_POST, EXTR_SKIP);
@extract($_FILES, EXTR_SKIP);
@extract($_GET, EXTR_SKIP);
@extract($_ENV, EXTR_SKIP);
}
REMOTE_ADDR is easier to understand, it is available in the PHP manual It means that it is a predetermined variable; as for HTTP_x_FORWARDED_FOR, I found some information on the Internet and said this
Use $_SERVER["REMOTE_ADDR"] in PHP to obtain the client's IP address, but if the client uses a proxy server to When accessing, what you get is the IP address of the proxy server, not the real client IP address. To obtain the client's real IP address through a proxy server, use $_SERVER["HTTP_X_FORWARDED_FOR"] to read it.
But something to note, not every proxy server can use $_SERVER["HTTP_X_FORWARDED_FOR"] to read the real IP of the client. Some of the IPs read by this method are still the IP of the proxy server.

As for HTTP_CLIENT_IP, a post said
'HTTP_CLIENT_IP' is the user's IP, 'HTTP_X_FORWARDED_FOR' is the proxy's IP
These IP header messages may not be available (because different browsers and different network devices may send different messages IP header message). So PHP tries to judge each IP header message, and if there is one, take one of them.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn