PHP client real IP address acquisition methods

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.