After complicated judgment and calculation, the IP address retrieval Function
CopyCode 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 codeCopy codeThe Code is as follows: $ reip = $ _ server ["remote_addr"];
Echo $ reip;
PHP obtains the IP addressAlgorithm Copy codeThe 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;
You can obtain the IP addresses of Intranet and external websites.Program Copy codeThe 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 processCopy codeThe 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
obtain the real IP address of the client in PHP