PHP external IP address

Source: Internet
Author: User

It mainly determines whether the client uses a proxy. Pay attention to the judgment order and first determine whether the client uses the proxy http_x_forwarded_for.

Function get_onlineip (){

$ Onlineip = '';
If (getenv ('HTTP _ client_ip ') & strcasecmp (getenv ('HTTP _ client_ip'), 'unknown ')){
$ Onlineip = getenv ('HTTP _ client_ip ');
} Elseif (getenv ('HTTP _ x_forwarded_for ') & strcasecmp (getenv ('HTTP _ x_forwarded_for'), 'unknown ')){
$ Onlineip = getenv ('HTTP _ x_forwarded_for ');
} Elseif (getenv ('remote _ ADDR ') & strcasecmp (getenv ('remote _ ADDR'), 'unknown ')){
$ Onlineip = getenv ('remote _ ADDR ');
} Elseif (isset ($ _ server ['remote _ ADDR ']) & $ _ server ['remote _ ADDR '] & strcasecmp ($ _ server ['remote _ ADDR'], 'unknown ')){
$ Onlineip = $ _ server ['remote _ ADDR '];
}
Return $ onlineip;
}

Function real_ip_fun ()
{
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 ']);
 

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 ');
}
}
 
Preg_match ("/[\ D \.] {7, 15}/", $ realip, $ onlineip );
$ Realip =! Empty ($ onlineip [0])? $ Onlineip [0]: '0. 0.0.0 ';
 
Return $ realip;
 
}

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.