PHP擷取瀏覽器資訊類和用戶端地理位置的2個方法_php執行個體

來源:互聯網
上載者:User

一、擷取瀏覽器資訊,擷取訪客作業系統:windows、mac、linux、unix、bsd、other,以及訪客ip地址等資訊的PHP類

複製代碼 代碼如下:

<?php 
/**
 * 擷取訪客資訊的類:語言、瀏覽器、作業系統、ip、地理位置、isp。
 * 使用:
 *   $obj = new guest_info;
 *   $obj->getlang();     //擷取訪客語言:簡體中文、繁體中文、english。
 *   $obj->getbrowser();  //擷取訪客瀏覽器:msie、firefox、chrome、safari、opera、other。
 *   $obj->getos();       //擷取訪客作業系統:windows、mac、linux、unix、bsd、other。
 *   $obj->getip();       //擷取訪客ip地址。
 *   $obj->getadd();      //擷取訪客地理位置,使用 baidu 隱藏介面。
 *   $obj->getisp();      //擷取訪客isp,使用 baidu 隱藏介面。
 */ 
class guest_info{ 
    function getlang() { 
        $lang = substr($_server['http_accept_language'], 0, 4); 
        //使用substr()截取字串,從 0 位開始,截取4個字元 
        if (preg_match('/zh-c/i',$lang)) { 
        //preg_match()Regex匹配函數 
            $lang = '簡體中文'; 
        }
        elseif (preg_match('/zh/i',$lang)) { 
            $lang = '繁體中文'; 
        } 
        else { 
            $lang = 'english'; 
        } 
        return $lang; 
    } 
    function getbrowser() { 
        $browser = $_server['http_user_agent']; 
        if (preg_match('/msie/i',$browser)) { 
            $browser = 'msie'; 
        } 
        elseif (preg_match('/firefox/i',$browser)) { 
            $browser = 'firefox'; 
        } 
        elseif (preg_match('/chrome/i',$browser)) { 
            $browser = 'chrome'; 
        } 
        elseif (preg_match('/safari/i',$browser)) { 
            $browser = 'safari'; 
        } 
        elseif (preg_match('/opera/i',$browser)) { 
            $browser = 'opera'; 
        } 
        else { 
            $browser = 'other'; 
        } 
        return $browser; 
    } 
    function getos() { 
        $os = $_server['http_user_agent']; 
        if (preg_match('/win/i',$os)) { 
            $os = 'windows'; 
        } 
        elseif (preg_match('/mac/i',$os)) { 
            $os = 'mac'; 
        } 
        elseif (preg_match('/linux/i',$os)) { 
            $os = 'linux'; 
        } 
        elseif (preg_match('/unix/i',$os)) { 
            $os = 'unix'; 
        } 
        elseif (preg_match('/bsd/i',$os)) { 
            $os = 'bsd'; 
        } 
        else { 
            $os = 'other'; 
        } 
        return $os; 
    } 
    function getip() { 
        if (!empty($_server['http_client_ip'])) { 
        //如果變數是非空或非零的值,則 empty()返回 false。 
            $ip = explode(',',$_server['http_client_ip']); 
        } 
        elseif (!empty($_server['http_x_forwarded_for'])) { 
            $ip = explode(',',$_server['http_x_forwarded_for']); 
        } 
        elseif (!empty($_server['remote_addr'])) { 
            $ip = explode(',',$_server['remote_addr']); 
        } 
        else { 
            $ip[0] = 'none'; 
        } 
        return $ip[0]; 
    }     
} 

$obj = new guest_info; 
echo    $obj->getlang();  //擷取訪客語言:簡體中文、繁體中文、english。 
echo    $obj->getbrowser(); //擷取訪客瀏覽器:msie、firefox、chrome、safari、opera、other。 
echo    $obj->getos();  //擷取訪客作業系統:windows、mac、linux、unix、bsd、other。 
echo    $obj->getip();  //擷取訪客ip地址。 
?>


二、php利用騰訊ip分享計劃擷取ip地理位置

複製代碼 代碼如下:

<?php 
function getiploc_qq($queryip){     
  $url = 'http://ip.qq.com/cgi-bin/searchip?searchip1='.$queryip;     
  $ch = curl_init($url);     
  curl_setopt($ch,curlopt_encoding ,'gb2312');   
  curl_setopt($ch, curlopt_timeout, 10);    
  curl_setopt($ch, curlopt_returntransfer, true) ; // 擷取資料返回   
  $result = curl_exec($ch);     
  $result = mb_convert_encoding($result, "utf-8", "gb2312"); // 編碼轉換,否則亂碼  
  curl_close($ch);    
  preg_match("@<span>(.*)</span></p>@iu",$result,$iparray);     
  $loc = $iparray[1];     
  return $loc; 
}  
//使用 
echo getiploc_qq("183.37.209.57"); //即可得到ip地址所在的地址位置。    
?>

三、php利用新浪ip查詢介面擷取ip地理位置

複製代碼 代碼如下:

<?php  
function getiploc_sina($queryip){     
   $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$queryip;     
   $ch = curl_init($url);      
   curl_setopt($ch,curlopt_encoding ,'utf8');      
   curl_setopt($ch, curlopt_timeout, 5);    
   curl_setopt($ch, curlopt_returntransfer, true) ; // 擷取資料返回   
   $location = curl_exec($ch);     
   $location = json_decode($location);     
   curl_close($ch);          
   $loc = "";    
   if($location===false) return "";      
      if (empty($location->desc)) {     
      $loc = $location->province.$location->city.$location->district.$location->isp;   
   }else{         $loc = $location->desc;     
   }     
    return $loc; 
} 
echo getiploc_sina("183.37.209.57"); 
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.