PHP can be used to obtain the real IP address of the client. For more information, see. After complicated judgment and calculation, the IP address retrieval function
The code is as follows:
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;
}
The simplest example of getting an IP address code
The code is as follows:
$ ReIP = $ _ SERVER ["REMOTE_ADDR"];
Echo $ reIP;
Php ip retrieval algorithm
The code is as follows:
If (getenv ('http _ CLIENT_IP ')){
$ Onlineip = getenv ('http _ CLIENT_IP ');
} Elseif (getenv ('http _ X_FORWARDED_FOR ')){
$ Onlineip = getenv ('http _ X_FORWARDED_FOR ');
} Elseif (getenv ('remote _ ADDR ')){
$ Onlineip = getenv ('remote _ ADDR ');
} Else {
$ Onlineip = $ HTTP_SERVER_VARS ['remote _ ADDR '];
}
Echo $ onlineip;
Attackers can obtain IP addresses of internal and external websites.
The code is as follows:
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 ();
What algorithm does php use to obtain ip addresses? Number expression to process
The code is as follows:
$ User_IP = ($ _ SERVER ["HTTP_VIA"])? $ _ SERVER ["HTTP_X_FORWARDED_FOR"]: $ _ SERVER ["REMOTE_ADDR"];
$ User_IP = ($ user_IP )? $ User_IP: $ _ SERVER ["REMOTE_ADDR"];
Function for obtaining the client IP address in php
Php obtains the real ip address of the client.