How to get the real ip of the user client in php

怪我咯
Release: 2023-03-12 19:18:02
Original
1163 people have browsed it

Some analysis of ideas for obtaining the real IP of the client may not be correct, but at least the accuracy is much better.

The code is as follows:

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); 
}
Copy after login

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. php manual states 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 access, what is obtained 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.
However, it should be noted that 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, there is a post saying that
'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 of different browsers Different network devices may send different IP header messages). So PHP tries to judge each IP header message, and if there is one, take one of them.

The above is the detailed content of How to get the real ip of the user client in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template