Blogger Information
Blog 9
fans 0
comment 0
visits 45608
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php获取***端真实ip地址的三种方法
Fire的博客
Original
2783 people have browsed it

php获取***端真实ip地址的三种方法

第一种方法,还算靠谱,本人以前一直用的是这个方法:

function get_real_ip(){
 $ip=false;
 if(!empty($_SERVER['HTTP_CLIENT_IP'])){
  $ip=$_SERVER['HTTP_CLIENT_IP'];
 }
 if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
  $ips=explode (', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
  if($ip){ array_unshift($ips, $ip); $ip=FALSE; }
  for ($i=0; $i < count($ips); $i++){
   if(!preg_match ('/^(10│172.16│192.168)./', $ips[$i])){
    $ip=$ips[$i];
    break;
   }
  }
 }
 return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}  

第二种方法:

function get_real_ip(){
   static $realip;
   if(isset($_SERVER)){
       if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
           $realip=$_SERVER['HTTP_X_FORWARDED_FOR'];
       }else if(isset($_SERVER['HTTP_CLIENT_IP'])){
           $realip=$_SERVER['HTTP_CLIENT_IP'];
       }else{
           $realip=$_SERVER['REMOTE_ADDR'];
       }
   }else{
       if(getenv('HTTP_X_FORWARDED_FOR')){
           $realip=getenv('HTTP_X_FORWARDED_FOR');
       }else if(getenv('HTTP_CLIENT_IP')){
           $realip=getenv('HTTP_CLIENT_IP');
       }else{
           $realip=getenv('REMOTE_ADDR');
       }
   }
   return $realip;
}
 

第三种方法,摘自DISCUZ,应该还不错吧!

// 获取IP地址(摘自discuz)
function getIp(){
 $ip='未知IP';
 if(!empty($_SERVER['HTTP_CLIENT_IP'])){
  return is_ip($_SERVER['HTTP_CLIENT_IP'])?$_SERVER['HTTP_CLIENT_IP']:$ip;
 }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
  return is_ip($_SERVER['HTTP_X_FORWARDED_FOR'])?$_SERVER['HTTP_X_FORWARDED_FOR']:$ip;
 }else{
  return is_ip($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:$ip;
 }
}
function is_ip($str){
 $ip=explode('.',$str);
 for($i=0;$i<count($ip);$i++){ 
  if($ip[$i]>255){ 
   return false; 
  } 
 } 
 return preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/',$str); 
}    

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments