Remote_addr can only obtain the IP address set in the visitor's local connection, for example, 10 configured on the campus network of Central South University for Nationalities. x. xxx. XXX series IP address. This function obtains the IP address at the LAN gateway egress,
If a visitor uses a proxy server, the visitor obtains the real IP address of the visitor gateway instead of the IP address of the proxy server. If you apply this function to a webpage with limited IP Access, other users will not be able to access the webpage even if they access the proxy server in the restricted IP segment.
// Define a function getip () {Global $ IP; If (getenv ("http_client_ip") $ IP = getenv ("http_client_ip "); else if (getenv ("register") $ IP = getenv ("http_x_forwarded_for"); else if (getenv ("remote_addr") $ IP = getenv ("remote_addr "); else $ IP = "unknow"; return $ IP;} // usage: Echo getip ();
Getenv ("remote_addr") is used to obtain the IP address of the client. However, if the client is accessed by the proxy server, the obtained IP address is the IP address of the proxy server rather than the real IP address of the client. To obtain the real IP address of the client through the proxy server, use getenv ("http_x_forwarded_for") to read.
However, if the client is not accessed through the proxy server, the value obtained using getenv ("http_x_forwarded_for") will be null.
Else if (getenv ("http_x_forwarded_for "))
$ IP = getenv ("http_x_forwarded_for ");
Indicates that if the value obtained by getenv ("http_x_forwarded_for") is not null (that is, when the client uses the proxy server), the variable $ IP is equal to getenv ("http_x_forwarded_for ") the actual IP address.
If the value obtained by the preceding else if (getenv ("http_x_forwarded_for") is null (that is, no proxy server is used ), the following $ IP = getenv ("http_x_forwarded_for") is not executed.
in this case, you have confirmed that the client does not use the proxy server, and then use
else if (getenv ("remote_addr "))
$ IP = getenv ("remote_addr");
the IP addresses of the clients obtained by these two statements are also real IP addresses.