Php obtains the IP address of the client.

Source: Internet
Author: User

Simplest practice

The code is as follows: Copy code


Function getRealIpAddr ()
{
If (! Emptyempty ($ _ SERVER ['http _ CLIENT_IP '])
  {
$ Ip = $ _ SERVER ['http _ CLIENT_IP '];
  }
Elseif (! Emptyempty ($ _ SERVER ['http _ X_FORWARDED_FOR '])
// To check ip is pass from proxy
  {
$ Ip = $ _ SERVER ['http _ X_FORWARDED_FOR '];
  }
Else
  {
$ Ip = $ _ SERVER ['remote _ ADDR '];
  }
Return $ ip;
}


I wrote this myself.

 

The code is as follows: Copy code
<? Php
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 '];
 }
Echo $ onlineip;
?>

However, Baidu found

Instance

The code is as follows: Copy code

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 ');
  }
 }

Preg_match ("/[d.] {7, 15}/", $ realip, $ onlineip );
$ Realip =! Empty ($ onlineip [0])? $ Onlineip [0]: '0. 0.0.0 ';

Return $ realip;
}

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.