How PHP obtains real IP

Source: Internet
Author: User
Tried a lot of code on the Internet is not working, but ip138 every time to show my real IP. Special Beg You heroes.

For example, I use normal PHP to get my IP address is: 60.55.8.229-Ningbo, Zhejiang

But ip38.com get my IP address is: [42.196.192.67] From: Shanghai Great Wall Broadband

Ip138 is the right one.

I am in the hospital test also this, own PHP code to obtain the IP is Zhejiang Hangzhou

But ip138 can get my real IP, Shanghai Jiaotong University

I now use the following PHP code:
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); } echo GetIP ();


Reply to discussion (solution)

Not studied, I want to know.
Look forward to the answer downstairs

Http://iframe.ip138.com/ic.asp

Maybe the ASP has magical functions?

? Is my method:

Function Curip () {$ip = ' ', IF (Getenv (' http_client_ip ') and strcasecmp (Getenv (' http_client_ip '), ' unknown ')) {$ip = Getenv (' http_client_ip ');} ElseIF (Getenv (' http_x_forwarded_for ') and strcasecmp (Getenv (' http_x_forwarded_for '), ' unknown ') {$ip =getenv (' HTTP _x_forwarded_for ');} ElseIF (Getenv (' remote_addr ') and strcasecmp (Getenv (' remote_addr '), ' unknown ') {$ip =getenv (' remote_addr ');} ElseIF (Isset ($_server[' REMOTE_ADDR ']) and $_server[' REMOTE_ADDR '] and strcasecmp ($_server[' remote_addr '), ' unknown ' ) {$ip =$_server[' remote_addr ');} else{$ip = ' 127.0.0.1 ';} Return $ip;}

function Real_ip ()
{
static $realip = NULL;

if ($realip!== NULL)
{
return $realip;
}

if (Isset ($_server))
{
if (Isset ($_server[' http_x_forwarded_for '))
{
$arr = Explode (', ', $_server[' http_x_forwarded_for ');

/* Take the first non-unknown valid IP string in X-forwarded-for */
foreach ($arr as $ip)
{
$ip = Trim ($IP);

if ($ip! = ' Unknown ')
{
$realip = $ip;

Break
}
}
}
ElseIf (Isset ($_server[' http_client_ip '))
{
$realip = $_server[' http_client_ip ');
}
Else
{
if (Isset ($_server[' remote_addr '))
{
$realip = $_server[' remote_addr ');
}
Else
{
$realip = ' 0.0.0.0 ';
}
}
}
Else
{
if (getenv (' http_x_forwarded_for '))
{
$realip = getenv (' http_x_forwarded_for ');
}
ElseIf (getenv (' http_client_ip '))
{
$realip = getenv (' http_client_ip ');
}
Else
{
$realip = getenv (' remote_addr ');
}
}
$ONLINEIP = null;
Preg_match ("/[\d\.") {7,15}/", $realip, $ONLINEIP);
$realip =!empty ($onlineip [0])? $ONLINEIP [0]: ' 0.0.0.0 ';

return $realip;
}

No matter how complicated your code is, it's all checked in turn.
Http_client_ip
Http_x_forwarded_for
Remote_addr
exists and ends once one exists

However, it is important to note that both HTTP_CLIENT_IP and http_x_forwarded_for are programmable
Like what
$ip = ' 888.888.888.888 ';
curl_setopt ($ch, Curlopt_httpheader,array ("X-forwarded-for: $ip", "Client-ip: $ip"));
Then your code will assume that IP is 888.888.888.888.
Apparently, it's a joke.

REMOTE_ADDR always reflects the first-level routed IP of the access network, which is the real IP

Real? No, I'm a proxy, you're stupid.

To break the proxy ... It's really a problem. Need other technology PHP bad implementation

The personal use of goagent Google's agent, 99% of all can not be measured out drops.

Using the functions in PHP can be implemented

The above code to get the IP method, there is no error, wrong in your server may be single-line.

I personally tested, the same PHP code, using the United States server, access to the real Great Wall broadband IP is wrong, the use of Hangzhou Ali Baba multi-line server, access and ip138, the same as the use of Xiamen Telecom line, the acquisition of IP is also wrong. Visible multi-line in the domestic is very important. Domestic is his grandmother's fucking, man-made a lot of obstacles.

  • Related Article

    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.