php获取客户端ip

 1 // 获取客户端IP地址
 2 function get_client_ip() {
 3     static $ip = NULL;
 4     if ($ip !== NULL)
 5         return $ip;
 6     if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
 7         $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
 8         $pos = array_search('unknown', $arr);
 9         if (false !== $pos)
10             unset($arr[$pos]);
11         $ip = trim($arr[0]);
12     }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
13         $ip = $_SERVER['HTTP_CLIENT_IP'];
14     } elseif (isset($_SERVER['REMOTE_ADDR'])) {
15         $ip = $_SERVER['REMOTE_ADDR'];
16     }
17     // IP地址合法验证
18     $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
19     return $ip;
20 }

 

posted @ 2014-04-02 13:37  猿来如些  阅读(176)  评论(0编辑  收藏  举报