PHP獲取瀏覽器,PHP獲取IP地址,PHP獲取設備型號


PHP獲取瀏覽器,PHP獲取IP地址,PHP獲取設備型號

 

<?php

//獲取瀏覽器

function get_broswer(){
    $sys = $_SERVER['HTTP_USER_AGENT'];  //獲取用戶代理字符串

    $exp[0] = "未知瀏覽器";
    $exp[1] = "";
  //stripos() 函數查找字符串在另一字符串中第一次出現的位置(不區分大小寫)    preg_match()執行匹配正則表達式
    if (stripos($sys, "Firefox/") > 0) {
         preg_match("/Firefox\/([^;)]+)+/i", $sys, $b);
         $exp[0] = "Firefox";
         $exp[1] = $b[1];  //獲取火狐瀏覽器的版本號
    }  if (stripos($sys, "Maxthon") > 0) {
         preg_match("/Maxthon\/([\d\.]+)/", $sys, $aoyou);
         $exp[0] = "傲游";
         $exp[1] = $aoyou[1];
    }  if (stripos($sys, "MSIE") > 0) {
         preg_match("/MSIE\s+([^;)]+)+/i", $sys, $ie);
         $exp[0] = "IE";
         $exp[1] = $ie[1];  //獲取IE的版本號
    }  if (stripos($sys, "OPR") > 0) {
             preg_match("/OPR\/([\d\.]+)/", $sys, $opera);
         $exp[0] = "Opera";
         $exp[1] = $opera[1];
    }  if(stripos($sys, "Edge") > 0) {
         //win10 Edge瀏覽器 添加了chrome內核標記 在判斷Chrome之前匹配
         preg_match("/Edge\/([\d\.]+)/", $sys, $Edge);
         $exp[0] = "Edge";
         $exp[1] = $Edge[1];
    }  if (stripos($sys, "Chrome") > 0) {
             preg_match("/Chrome\/([\d\.]+)/", $sys, $google);
         $exp[0] = "Chrome";
         $exp[1] = $google[1];  //獲取google chrome的版本號
    }  if(stripos($sys,'rv:')>0 && stripos($sys,'Gecko')>0){
         preg_match("/rv:([\d\.]+)/", $sys, $IE);
             $exp[0] = "IE";
         $exp[1] = $IE[1];
    }
    return $exp[0].'('.$exp[1].')';
}





//獲取操作系統
function get_os(){
    $agent = $_SERVER['HTTP_USER_AGENT'];
    $os = '未知操作系統';

    if (preg_match('/win/i', $agent) && strpos($agent, '95'))
    {
      $os = 'Windows 95';
    }
      if (preg_match('/win 9x/i', $agent) && strpos($agent, '4.90'))
    {
      $os = 'Windows ME';
    }
      if (preg_match('/win/i', $agent) && preg_match('/98/i', $agent))
    {
      $os = 'Windows 98';
    }
    if (preg_match('/win/i', $agent) && preg_match('/nt/i', $agent))
    {
      $os = 'Windows NT';
    }
    if (preg_match('/win/i', $agent) && preg_match('/nt 6.0/i', $agent))
    {
      $os = 'Windows Vista';
    }
    if (preg_match('/win/i', $agent) && preg_match('/nt 6.1/i', $agent))
    {
      $os = 'Windows 7';
    }
    if (preg_match('/win/i', $agent) && preg_match('/nt 6.2/i', $agent))
    {
      $os = 'Windows 8';
    }
    if(preg_match('/win/i', $agent) && preg_match('/nt 10.0/i', $agent))
    {
      $os = 'Windows 10';#添加win10判斷
    }
    if (preg_match('/win/i', $agent) && preg_match('/nt 5.1/i', $agent))
    {
      $os = 'Windows XP';
    }
    if (preg_match('/win/i', $agent) && preg_match('/nt 5/i', $agent))
    {
      $os = 'Windows 2000';
    }

    if (preg_match('/win/i', $agent) && preg_match('/32/i', $agent))
    {
      $os = 'Windows 32';
    }
    if (preg_match('/linux/i', $agent))
    {
      $os = 'Linux';
    }
    if (preg_match('/unix/i', $agent))
    {
      $os = 'Unix';
    }
    if (preg_match('/sun/i', $agent) && preg_match('/os/i', $agent))
    {
      $os = 'SunOS';
    }
      if (preg_match('/ibm/i', $agent) && preg_match('/os/i', $agent))
    {
      $os = 'IBM OS/2';
    }
    if (preg_match('/Mac/i', $agent) && preg_match('/PC/i', $agent))
    {
      $os = 'Macintosh';
    }
    if (preg_match('/PowerPC/i', $agent))
    {
      $os = 'PowerPC';
    }
    if (preg_match('/AIX/i', $agent))
    {
      $os = 'AIX';
    }
    if (preg_match('/HPUX/i', $agent))
    {
      $os = 'HPUX';
    }
    if (preg_match('/NetBSD/i', $agent))
    {
      $os = 'NetBSD';
    }
    if (preg_match('/BSD/i', $agent))
    {
      $os = 'BSD';
    }
    if (preg_match('/OSF1/i', $agent))
    {
      $os = 'OSF1';
    }
    if (preg_match('/IRIX/i', $agent))
    {
      $os = 'IRIX';
    }
    if (preg_match('/FreeBSD/i', $agent))
    {
      $os = 'FreeBSD';
    }
    if (preg_match('/teleport/i', $agent))
    {
      $os = 'teleport';
    }
    if (preg_match('/flashget/i', $agent))
    {
      $os = 'flashget';
    }
      if (preg_match('/webzip/i', $agent))
    {
      $os = 'webzip';
    }
      if (preg_match('/offline/i', $agent))
    {
      $os = 'offline';
    }
     if(strpos($agent, 'iphone')){
      $os = 'iphone';
    }
     if(strpos($agent, 'ipad')){
      $os = 'ipad';
    }
     if(strpos($agent, 'android')){
      $os = 'android';
    }
    if (stripos($agent, "SAMSUNG")!==false || stripos($agent, "Galaxy")!==false || strpos($agent, "GT-")!==false || strpos($agent, "SCH-")!==false || strpos($agent, "SM-")!==false) {
        $os = 'android ->三星';
    }
    if (stripos($agent, "Huawei")!==false || stripos($agent, "Honor")!==false || stripos($agent, "H60-")!==false || stripos($agent, "H30-")!==false) {
        $os = 'android ->華為';
    }
    if (stripos($agent, "Lenovo")!==false) {
        $os = 'android ->聯想';
    }
    if (strpos($agent, "MI-ONE")!==false || strpos($agent, "MI 1S")!==false || strpos($agent, "MI 2")!==false || strpos($agent, "MI 3")!==false || strpos($agent, "MI 4")!==false || strpos($agent, "MI-4")!==false) {
        $os = 'android ->小米';
    }
    if (strpos($agent, "HM NOTE")!==false || strpos($agent, "HM201")!==false) {
        $os = 'android ->紅米';
    }
    if (stripos($agent, "Coolpad")!==false || strpos($agent, "8190Q")!==false || strpos($agent, "5910")!==false) {
        $os = 'android ->酷派';
    }
    if (stripos($agent, "ZTE")!==false || stripos($agent, "X9180")!==false || stripos($agent, "N9180")!==false || stripos($agent, "U9180")!==false) {
        $os = 'android ->中興';
    }
    if (stripos($agent, "OPPO")!==false || strpos($agent, "X9007")!==false || strpos($agent, "X907")!==false || strpos($agent, "X909")!==false || strpos($agent, "R831S")!==false || strpos($agent, "R827T")!==false || strpos($agent, "R821T")!==false || strpos($agent, "R811")!==false || strpos($agent, "R2017")!==false) {
        $os = 'android ->OPPO';
    }
    if (strpos($agent, "HTC")!==false || stripos($agent, "Desire")!==false) {
        $os = 'android ->HTC';
    }
    if (stripos($agent, "vivo")!==false) {
        $os = 'android ->vivo';
    }
    if (stripos($agent, "K-Touch")!==false) {
        $os = 'android ->天語';
    }
    if (stripos($agent, "Nubia")!==false || stripos($agent, "NX50")!==false || stripos($agent, "NX40")!==false) {
        $os = 'android ->努比亞';
    }
    if (strpos($agent, "M045")!==false || strpos($agent, "M032")!==false || strpos($agent, "M355")!==false) {
        $os = 'android ->魅族';
    }
    if (stripos($agent, "DOOV")!==false) {
        $os = 'android ->朵唯';
    }
    if (stripos($agent, "GFIVE")!==false) {
        $os = 'android ->基伍';
    }
    if (stripos($agent, "Gionee")!==false || strpos($agent, "GN")!==false) {
        $os = 'android ->金立';
    }
    if (stripos($agent, "HS-U")!==false || stripos($agent, "HS-E")!==false) {
        $os = 'android ->海信';
    }
    if (stripos($agent, "Nokia")!==false) {
        $os = 'android ->諾基亞';
    }
    return $os;
}



/**
 * 是否移動端訪問訪問
 * 判斷當前訪問的用戶是  PC端  還是 手機端  返回true 為手機端  false 為PC 端
 * @return boolean
 */
function isMobile(){
        $code = 2;
        // 如果有HTTP_X_WAP_PROFILE則一定是移動設備
        if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
        {
            $code = 1;
        }
        // 如果via信息含有wap則一定是移動設備,部分服務商會屏蔽該信息
        if (isset ($_SERVER['HTTP_VIA']))
        {
            // 找不到為flase,否則為true
            $code = stristr($_SERVER['HTTP_VIA'], "wap") ? 1 : 2;
        }
        // 腦殘法,判斷手機發送的客戶端標志,兼容性有待提高
        if (isset ($_SERVER['HTTP_USER_AGENT']))
        {
            $clientkeywords = array ('nokia',
                'sony',
                'ericsson',
                'mot',
                'samsung',
                'htc',
                'sgh',
                'lg',
                'sharp',
                'sie-',
                'philips',
                'panasonic',
                'alcatel',
                'lenovo',
                'iphone',
                'ipod',
                'blackberry',
                'meizu',
                'android',
                'netfront',
                'symbian',
                'ucweb',
                'windowsce',
                'palm',
                'operamini',
                'operamobi',
                'openwave',
                'nexusone',
                'cldc',
                'midp',
                'wap',
                'mobile'
                );
            // 從HTTP_USER_AGENT中查找手機瀏覽器的關鍵字
            if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
            {
                $code = 1;
            }
        }
        // 協議法,因為有可能不准確,放到最后判斷
        if (isset ($_SERVER['HTTP_ACCEPT']))
        {
            // 如果只支持wml並且不支持html那一定是移動設備
            // 如果支持wml和html但是wml在html之前則是移動設備
            if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
            {
                $code = 1;
            }
        }
        return $code;
}


/**
 * 獲取客戶端IP地址
 * @param integer $type 返回類型 0 返回IP地址 1 返回IPV4地址數字
 * @param boolean $adv 是否進行高級模式獲取(有可能被偽裝)
 * @return string
 */
function get_client_ip()
{
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
        $ip = getenv("HTTP_CLIENT_IP");
      if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
      if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
        $ip = getenv("REMOTE_ADDR");
      if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
        $ip = $_SERVER['REMOTE_ADDR'];

       if(empty($ip))$ip = "unknown";
    return ($ip);
}


//獲得訪客真實ip
    function Getip()
 
         
   {
 
         
     if (! empty ( $_SERVER [ "HTTP_CLIENT_IP" ])) {
 
         
       $ip = $_SERVER [ "HTTP_CLIENT_IP" ];
 
         
     }
 
         
     if (! empty ( $_SERVER [ 'HTTP_X_FORWARDED_FOR' ])) { // 獲取代理ip
 
         
       $ips = explode ( ',' , $_SERVER [ 'HTTP_X_FORWARDED_FOR' ]);
 
         
     }
 
         
     if ( $ip ) {
 
         
       $ips = array_unshift ( $ips , $ip );
 
         
     }
 
         
     $count = count ( $ips );
 
         
     for ( $i = 0; $i < $count ; $i ++) {
 
         
       if (! preg_match( "/^(10|172\.16|192\.168)\./i" , $ips [ $i ])) { // 排除局域網ip
 
         
         $ip = $ips [ $i ];
 
         
         break ;
 
         
       }
 
         
     }
 
         
     $tip = empty ( $_SERVER [ 'REMOTE_ADDR' ]) ? $ip : $_SERVER [ 'REMOTE_ADDR' ];
 
         
     if ( $tip == "127.0.0.1" ) { // 獲得本地真實IP
 
         
       return $this ->get_onlineip();
 
         
     } else {
 
         
       return $tip ;
 
         
     }
 
         
   }
 
 
 
    // //根據ip獲得訪客所在地地名
   function Getaddress( $ip = '' )
   {
     if ( empty ( $ip )) {
       $ip = $this ->Getip();
     }
     $ipadd = file_get_contents ( "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" . $ip ); // 根據新浪api接口獲取
     if ( $ipadd ) {
       $charset = iconv( "gbk" , "utf-8" , $ipadd );
       preg_match_all( "/[\x{4e00}-\x{9fa5}]+/u" , $charset , $ipadds );
       return $ipadds ; // 返回一個二維數組
     } else {
       return "addree is none" ;
     }
   }
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM