Copy codeThe 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 );
}
Regist = off
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 );
}
Among them, REMOTE_ADDR is easy to understand. The php manual shows that it is a predetermined variable, while HTTP_x_FORWARDED_FOR finds some information on the Internet.
Use $ _ SERVER ["REMOTE_ADDR"] in PHP to obtain the IP address of the client. However, if the client is accessed by a proxy SERVER, the IP address of the proxy SERVER is obtained, instead of the real client IP address. To obtain the real IP address of the client through the proxy SERVER, use $ _ SERVER ["HTTP_X_FORWARDED_FOR"] to read it.
However, not every proxy SERVER can use $ _ SERVER ["HTTP_X_FORWARDED_FOR"] to read the real IP address of the client, some of the IP addresses read by this method are still the Proxy Server IP addresses.
As for HTTP_CLIENT_IP, there is a post
'Http _ CLIENT_IP 'is the user's IP address, and 'HTTP _ X_FORWARDED_FOR' is the proxy's IP address.
These IP header messages may not be obtained (because different browsers may send different IP header messages to different network devices ). so PHP tries to judge each IP header message. If yes, take one of them.