根據UA簡單判斷訪客客戶端是手機移動端還是電腦PC端

皇蟲發表於2016-12-15

畢竟手機端的UA太多也太雜亂了,一個個去找太蛋疼,索性從別人PHP判斷手機端還是PC端的函式程式碼抄襲過來了,先附上Golang程式碼然後附上再PHP程式碼。

Golang程式碼:

package main

import (
    "fmt"
    "strings"
)

func main() {
    ua_pc := "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36"
    ua_mobile := "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"
    fmt.Println("手機端?", Client(ua_pc))
    fmt.Println("手機端?", Client(ua_mobile))
}

//根據ua判斷訪客是手機端還是PC端訪問
//手機端返回true,否則返回false,預設返回false,即PC端
func Client(ua string) bool {
    ua = strings.ToLower(ua)
    //常見的手機端UA判斷,有即預設為手機端 pad、phone關鍵字替代ipad,iphone,apad等
    mobile := []string{"mobile", "phone", "android", "pad", "pod", "symbian", "wap", "smartphone", "apk", "ios"}
    for _, m := range mobile {
        if strings.Contains(ua, m) {
            return true
        }
    }
    //生僻的不常見的UA判斷
    mbstr := "w3c,acs-,alav,alca,amoi,audi,avan,benq,bird,blac"
    mbstr += "blaz,brew,cell,cldc,cmd-,dang,doco,eric,hipt,inno"
    mbstr += "ipaq,java,jigs,kddi,keji,leno,lg-c,lg-d,lg-g,"
    mbstr += "maui,maxo,midp,mits,mmef,mobi,mot-,moto,mwbp,"
    mbstr += "newt,noki,oper,palm,pana,pant,phil,play,port,prox"
    mbstr += "qwap,sage,sams,sany,sch-,sec-,send,seri,sgh-,shar"
    mbstr += "sie-,siem,smal,smar,sony,sph-,symb,t-mo,teli,"
    mbstr += "tosh,tsm-,upg1,upsi,vk-v,voda,wap-,wapa,wapi,wapp"
    mbstr += "wapr,webc,winw,winw,xda,xda-,up.browser,up.link,mmp,midp,xoom"
    slice := strings.Split(mbstr, ",")
    for _, m := range slice {
        if strings.Contains(ua, m) {
            return true
        }
    }
    return false
}

PHP程式碼:

/**
 * 檢測是否是手機端
 * @return boolean  */
function is_mobile(){
    $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';
    $mobile_browser = '0';
    if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_SERVER['HTTP_USER_AGENT'])))
        $mobile_browser++;
    if((isset($_SERVER['HTTP_ACCEPT'])) and (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false))
        $mobile_browser++;
    if(isset($_SERVER['HTTP_X_WAP_PROFILE']))
        $mobile_browser++;
    if(isset($_SERVER['HTTP_PROFILE']))
        $mobile_browser++;
    $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
    $mobile_agents = array(
            'w3c ','','alav','alca','amoi','audi','avan','benq','bird','blac',
            'blaz','brew','cell','cldc','','dang','doco','eric','hipt','inno',
            'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','',
            'maui','maxo','midp','mits','mmef','mobi','','moto','mwbp','',
            'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
            'qwap','sage','sams','sany','','','send','seri','','shar',
            '','siem','smal','smar','sony','','symb','t-mo','teli','',
            'tosh','','upg1','upsi','vk-v','voda','','wapa','wapi','wapp',
            'wapr','webc','winw','winw','xda',''
    );
    if(in_array($mobile_ua, $mobile_agents)) $mobile_browser++;
    if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false) $mobile_browser++;
    // Pre-final check to reset everything if the user is on Windows
    if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false) $mobile_browser=0;
    // But WP7 is also Windows, with a slightly different characteristic
    if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false) $mobile_browser++;
    if($mobile_browser>0) return true;
    return false;
}

相關文章