(1). REMOTE_ADDR: IP address of the user's computer browsing the current page
(2). HTTP_X_FORWARDED_FOR: view the gateway of the user's computer on the current page
(3). HTTP_CLIENT_IP: client ip address
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.
Note that if the client is not accessed through the proxy SERVER, the value obtained from $ _ SERVER ["HTTP_X_FORWARDED_FOR"] will be null.
Instance
// Function for obtaining IP addresses after complicated judgment and calculation
Function getIP (){
If (getenv ('http _ CLIENT_IP ')){
$ Ip = getenv ('http _ CLIENT_IP ');
}
Elseif (getenv ('http _ X_FORWARDED_FOR ')){
$ Ip = getenv ('http _ X_FORWARDED_FOR ');
}
Elseif (getenv ('http _ x_forwarded ')){
$ Ip = getenv ('http _ x_forwarded ');
}
Elseif (getenv ('http _ FORWARDED_FOR ')){
$ Ip = getenv ('http _ FORWARDED_FOR ');
}
Elseif (getenv ('http _ forwarded ')){
$ Ip = getenv ('http _ forwarded ');
}
Else {
$ Ip = $ _ SERVER ['remote _ ADDR '];
}
Return $ ip;
For more details, see php Tutorial er/21/9 a34bfce63448271c7b52fa168c3e40f.htm "> http://www.111cn.net/phper/21/9a34bfce63448271c7b52fa168c3e40f.htm
// You can obtain the IP addresses of an intranet or an external website.
Function getip_out (){
$ Ip = false;
If (! Empty ($ _ SERVER ["HTTP_CLIENT_IP"]) {
$ Ip = $ _ SERVER ["HTTP_CLIENT_IP"];
}
If (! Empty ($ _ SERVER ['http _ X_FORWARDED_FOR ']) {
$ Ips tutorial = explode (",", $ _ SERVER ['http _ X_FORWARDED_FOR ']);
If ($ ip) {array_unshift ($ ips, $ ip); $ ip = FALSE ;}
For ($ I = 0; $ I <count ($ ips); $ I ++ ){
If (! Eregi ("^ (10 │ 172.16 │ 192.168).", $ ips [$ I]) {
$ Ip = $ ips [$ I];
Break;
}
}
}
Return ($ ip? $ Ip: $ _ SERVER ['remote _ ADDR ']);
}
Echo getip_out ();
For more details, see: http://www.111cn.net/phper/21/9a34bfce63448271c7b52fa168c3e40f.htm