客户端的ip、地理位置、浏览器、以及访问设备

来源:互联网 发布:淘宝收货时间是多久 编辑:程序博客网 时间:2024/06/06 09:26
  1. <?php  
  2. header("Content:Content-type:text/html;charset=utf-8");  
  3. //      // 作用取得客户端的ip、地理位置、浏览器、以及访问设备  
  4.      class get_equipment_info{   
  5.       ////获得访客浏览器类型  
  6.       function GetBrowser(){  
  7.        if(!empty($_SERVER['HTTP_USER_AGENT']))  
  8.        {  
  9.          $br = $_SERVER['HTTP_USER_AGENT'];  
  10.           if (preg_match('/MSIE/i',$br)){      
  11.              $br = 'MSIE';  
  12.           }  
  13.           elseif (preg_match('/Firefox/i',$br)){  
  14.              $br = 'Firefox';  
  15.           }elseif (preg_match('/Chrome/i',$br)){  
  16.              $br = 'Chrome';  
  17.           }elseif (preg_match('/Safari/i',$br)){  
  18.              $br = 'Safari';  
  19.           }elseif (preg_match('/Opera/i',$br)){  
  20.              $br = 'Opera';  
  21.           }else {  
  22.              $br = 'Other';  
  23.           }  
  24.              return json_encode("浏览器为".$br);  
  25.           }else{  
  26.              return "获取浏览器信息失败!";}   
  27.       }  
  28.       ////获得访客浏览器语言  
  29.       function GetLang()  
  30.       {  
  31.            if(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])){  
  32.                $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];  
  33.                $lang = substr($lang,0,5);  
  34.                 if(preg_match("/zh-cn/i",$lang)){  
  35.                    $lang = "简体中文";  
  36.                 }elseif(preg_match("/zh/i",$lang)){  
  37.                    $lang = "繁体中文";  
  38.                 }else{  
  39.                    $lang = "English";  
  40.                 }  
  41.                 return json_encode("浏览器语言为".$lang);   
  42.            }else{  
  43.             return "获取浏览器语言失败!";  
  44.             }  
  45.       }  
  46.          
  47.      //获取客户端操作系统信息包括win10  
  48.     function GetOs(){  
  49.         $agent = $_SERVER['HTTP_USER_AGENT'];  
  50.         $os = false;  
  51.       
  52.         if (preg_match('/win/i', $agent) && strpos($agent, '95'))  
  53.         {  
  54.             $os = 'Windows 95';  
  55.         }  
  56.         else if (preg_match('/win 9x/i', $agent) && strpos($agent, '4.90'))  
  57.         {  
  58.             $os = 'Windows ME';  
  59.         }  
  60.         else if (preg_match('/win/i', $agent) && preg_match('/98/i', $agent))  
  61.         {  
  62.             $os = 'Windows 98';  
  63.         }  
  64.         else if (preg_match('/win/i', $agent) && preg_match('/nt 6.0/i', $agent))  
  65.         {  
  66.             $os = 'Windows Vista';  
  67.         }  
  68.         else if (preg_match('/win/i', $agent) && preg_match('/nt 6.1/i', $agent))  
  69.         {  
  70.             $os = 'Windows 7';  
  71.         }  
  72.         else if (preg_match('/win/i', $agent) && preg_match('/nt 6.2/i', $agent))  
  73.         {  
  74.             $os = 'Windows 8';  
  75.         }else if(preg_match('/win/i', $agent) && preg_match('/nt 10.0/i', $agent))  
  76.         {  
  77.             $os = 'Windows 10';#添加win10判断  
  78.         }else if (preg_match('/win/i', $agent) && preg_match('/nt 5.1/i', $agent))  
  79.         {  
  80.             $os = 'Windows XP';  
  81.         }  
  82.         else if (preg_match('/win/i', $agent) && preg_match('/nt 5/i', $agent))  
  83.         {  
  84.             $os = 'Windows 2000';  
  85.         }  
  86.         else if (preg_match('/win/i', $agent) && preg_match('/nt/i', $agent))  
  87.         {  
  88.             $os = 'Windows NT';  
  89.         }  
  90.         else if (preg_match('/win/i', $agent) && preg_match('/32/i', $agent))  
  91.         {  
  92.             $os = 'Windows 32';  
  93.         }  
  94.         else if (preg_match('/linux/i', $agent))  
  95.         {  
  96.             $os = 'Linux';  
  97.         }  
  98.         else if (preg_match('/unix/i', $agent))  
  99.         {  
  100.             $os = 'Unix';  
  101.         }  
  102.         else if (preg_match('/sun/i', $agent) && preg_match('/os/i', $agent))  
  103.         {  
  104.             $os = 'SunOS';  
  105.         }  
  106.         else if (preg_match('/ibm/i', $agent) && preg_match('/os/i', $agent))  
  107.         {  
  108.             $os = 'IBM OS/2';  
  109.         }  
  110.         else if (preg_match('/Mac/i', $agent) && preg_match('/PC/i', $agent))  
  111.         {  
  112.             $os = 'Macintosh';  
  113.         }  
  114.         else if (preg_match('/PowerPC/i', $agent))  
  115.         {  
  116.             $os = 'PowerPC';  
  117.         }  
  118.         else if (preg_match('/AIX/i', $agent))  
  119.         {  
  120.             $os = 'AIX';  
  121.         }  
  122.         else if (preg_match('/HPUX/i', $agent))  
  123.         {  
  124.             $os = 'HPUX';  
  125.         }  
  126.         else if (preg_match('/NetBSD/i', $agent))  
  127.         {  
  128.             $os = 'NetBSD';  
  129.         }  
  130.         else if (preg_match('/BSD/i', $agent))  
  131.         {  
  132.             $os = 'BSD';  
  133.         }  
  134.         else if (preg_match('/OSF1/i', $agent))  
  135.         {  
  136.             $os = 'OSF1';  
  137.         }  
  138.         else if (preg_match('/IRIX/i', $agent))  
  139.         {  
  140.             $os = 'IRIX';  
  141.         }  
  142.         else if (preg_match('/FreeBSD/i', $agent))  
  143.         {  
  144.             $os = 'FreeBSD';  
  145.         }  
  146.         else if (preg_match('/teleport/i', $agent))  
  147.         {  
  148.             $os = 'teleport';  
  149.         }  
  150.         else if (preg_match('/flashget/i', $agent))  
  151.         {  
  152.             $os = 'flashget';  
  153.         }  
  154.         else if (preg_match('/webzip/i', $agent))  
  155.         {  
  156.             $os = 'webzip';  
  157.         }  
  158.         else if (preg_match('/offline/i', $agent))  
  159.         {  
  160.             $os = 'offline';  
  161.         }  
  162.         else  
  163.         {  
  164.             $os = '未知操作系统';  
  165.         }  
  166.         return json_encode("系统为".$os);   
  167.     }  
  168.     //获得访客真实ip  
  169.       function Getip()  
  170.     {  
  171.         if (! empty($_SERVER["HTTP_CLIENT_IP"])) {  
  172.             $ip = $_SERVER["HTTP_CLIENT_IP"];  
  173.         }  
  174.         if (! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { // 获取代理ip  
  175.             $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);  
  176.         }  
  177.         if ($ip) {  
  178.             $ips = array_unshift($ips, $ip);  
  179.         }  
  180.           
  181.         $count = count($ips);  
  182.         for ($i = 0; $i < $count; $i ++) {  
  183.             if (! preg_match("/^(10|172\.16|192\.168)\./i", $ips[$i])) { // 排除局域网ip  
  184.                 $ip = $ips[$i];  
  185.                 break;  
  186.             }  
  187.         }  
  188.         $tip = empty($_SERVER['REMOTE_ADDR']) ? $ip : $_SERVER['REMOTE_ADDR'];  
  189.         if ($tip == "127.0.0.1") { // 获得本地真实IP  
  190.             return $this->get_onlineip();  
  191.         } else {  
  192.             return $tip;  
  193.         }  
  194.     }  
  195.      // //根据ip获得访客所在地地名  
  196.     function Getaddress($ip = '')  
  197.     {  
  198.         if (empty($ip)) {  
  199.             $ip = $this->Getip();  
  200.         }  
  201.         $ipadd = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" . $ip); // 根据新浪api接口获取  
  202.         if ($ipadd) {  
  203.             $charset = iconv("gbk", "utf-8", $ipadd);  
  204.             preg_match_all("/[\x{4e00}-\x{9fa5}]+/u", $charset, $ipadds);  
  205.               
  206.             return $ipadds; // 返回一个二维数组  
  207.         } else {  
  208.             return "addree is none";  
  209.         }  
  210.     }  
  211.     //获得本地真实IP  
  212.     // function get_onlineip()  
  213.     // {  
  214.     //     $mip = file_get_contents("http://city.ip138.com/city0.asp");  
  215.     //     if ($mip) {  
  216.     //         preg_match("/
    .
    /", $mip, $sip);  
  217.     //         $p = array(  
  218.     //             "/\[/",  
  219.     //             "/\]/"  
  220.     //         );  
  221.     //         return preg_replace($p, "", $sip[0]);  
  222.     //     } else {  
  223.     //         return "获取本地IP失败!";  
  224.     //     }  
  225.     // }  
  226. }  
  227.   
  228.  // $info = new get_equipment_info();  
  229.  //     echo json_decode($info -> GetLang());  
  230.  //     echo json_decode($info -> GetOs());  
  231.  //     echo json_decode($info -> GetBrowser());  
  232.  //     print_r($info -> Getaddress());  
  233.  //     echo $info -> Getip();  
  234.  //     echo $info -> get_onlineip();  
  235.  //     die;  
  236. ?>  
原创粉丝点击