How does php obtain the real IP address?

Source: Internet
Author: User
I tried to use php to obtain the real IP address. many codes on the Internet won't work, but ip138 can display my real ip address every time. Thank you for your support. For example, the IP address I obtained using the common php is: 60.55.8.229 & nbsp;-& nbsp; the IP address obtained by ip38.com in Ningbo, Zhejiang province is [42.196.192.67] & nbsp; from: shanghai & nbsp; Great Wall broadband ip138 is correct. How to obtain real IP addresses in php
I tried a lot of code on the Internet, but ip138 can display my real ip address every time. Thank you for your support.

For example, if my IP address is 60.55.8.229-Ningbo, Zhejiang province, the IP address obtained by using common php is 60.55.8.229.

However, the IP address obtained by ip38.com is [42.196.192.67] from: Shanghai Great Wall Broadband

Ip138 is correct.

The same is true for my tests in the hospital. the IP address obtained by my php code is Hangzhou, Zhejiang province.

But ip138 can get my real ip address, Shanghai Jiao Tong University

The PHP code I use now is as follows:
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();


------ Solution --------------------
Er, it's 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;
}

------ Solution --------------------
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 ']);

/* Obtain the first non-unknown valid IP string in X-Forwarded-*/
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;
}
------ Solution --------------------

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.