新手问题 根据UA简单判断访客客户端是手机移动端还是电脑PC端

1272881215 · 2016年12月15日 · 601 次阅读

毕竟手机端的 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,lge-"
    mbstr += "maui,maxo,midp,mits,mmef,mobi,mot-,moto,mwbp,nec-"
    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,tim-"
    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 ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
            'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
            'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
            'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
            'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
            'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
            'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
            'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
            'wapr','webc','winw','winw','xda','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;
}
更多原创文章干货分享,请关注公众号
  • 加微信实战群请加微信(注明:实战群):gocnio
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册