php获取ip地址(转)

  1. <?php   
  2. /*  
  3.  * Copyright (c) 2006-2008 coderhome.net  
  4.  * All rights reserved.  
  5.  * Support : 志凡(dzjzmj@163.com)  
  6.  *  
  7.  * Version :  1.0  
  8.  */  
  9. class Ip {   
  10.     public static function get() {   
  11.         if ($_SERVER['HTTP_CLIENT_IP'] && $_SERVER['HTTP_CLIENT_IP']!='unknown') {   
  12.             $ip = $_SERVER['HTTP_CLIENT_IP'];   
  13.         } elseif ($_SERVER['HTTP_X_FORWARDED_FOR'] && $_SERVER['HTTP_X_FORWARDED_FOR']!='unknown') {   
  14.             $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];   
  15.         } else {   
  16.             $ip = $_SERVER['REMOTE_ADDR'];   
  17.         }   
  18.         return $ip;   
  19.     }   
  20.        
  21.     public static function ipToInt($ip) {   
  22.         $ips = explode('.',$ip);   
  23.         if (count($ips)>=4) {   
  24.             $int = $ips[0]*256*256*256+$ips[1]*256*256+$ips[2]*256+$ips[3];   
  25.         } else {   
  26.             throw new Exception('ip is error');   
  27.         }   
  28.         return $int;   
  29.     }   
  30.        
  31.     public static function isIn($startIp$endIp$ip) {   
  32.         $start = Ip::ipToInt($startIp);   
  33.         $end = Ip::ipToInt($endIp);   
  34.         $ipInt = Ip::ipToInt($ip);   
  35.         $result = false;   
  36.         if ($ipInt>=$start && $ipInt<=$end) {   
  37.             $result = true;   
  38.         }   
  39.         return $result;   
  40.     }   
  41. }   
  42. ?>  

posted on 2008-08-25 12:25  y轴  阅读(1035)  评论(0编辑  收藏  举报

导航