Php is a good way to obtain the user IP address. REMOTE_ADDR can only obtain the IP address set in the visitor's local connection, for example, 10 configured on a college campus network. x. XXX. XXX Series IP address, and this function obtains the IP address REMOTE_ADDR at the LAN gateway egress. only the IP address set in the visitor's local connection can be obtained, for example, 10 configured in a college campus network. x. XXX. XXX Series IP address. this function obtains the IP address of the Lan gateway egress. if a visitor uses a proxy server, the visitor does not obtain the IP address of the proxy server, but the real IP address of the visitor gateway. 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.
The following provides a function:
The code is as follows:
// Define a function getIP ()
Function getIP ()
{
Global $ ip;
If (getenv ("HTTP_CLIENT_IP "))
$ Ip = getenv ("HTTP_CLIENT_IP ");
Else if (getenv ("HTTP_X_FORWARDED_FOR "))
$ 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.
The code is as follows:
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, it has been confirmed that the client does not use the proxy server
The code is as follows:
Else if (getenv ("REMOTE_ADDR "))
$ Ip = getenv ("REMOTE_ADDR ");
These two lines of statements obtain the client's IP address, which is also the real IP address.
...