判断浏览器打开移动端还是PC端

public function isMobile(){
    
    
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
    
    
        $is_mobile = true;
        return $is_mobile;
    }
    // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset($_SERVER['HTTP_VIA'])) {
    
    
        // 找不到为flase,否则为true
        $is_mobile = false;
        if (stristr($_SERVER['HTTP_VIA'], "wap")){
    
    
            $is_mobile = true;
        }
        return $is_mobile;
    }
    // 脑残法,判断手机发送的客户端标志,兼容性有待提高。其中'MicroMessenger'是电脑微信
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    if (isset($user_agent)) {
    
    
        $mobile_agents=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', 'MicroMessenger',
            "240x320","acer","acoon","acs-","abacho","ahong","airness","amoi","anywhereyougo.com","applewebkit","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo",
            "fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","huawei","hutchison","inno","ipad","ipaq","jbrowser","kddi","kgt","kwc","longcos","maemo","mercator","meridian","micromax","mini","mitsu","mmm","mmp","mobi","moto","nec-","newgen","nexian","nf-browser","nintendo","nitro","nook","novarra","obigo","pantech","phone","pg-",
            "sch-","scooter","sec-","sendo","sgh-","siemens","sie-","softbank","spice","sprint","spv","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wigbrowser","wii","wireless","xda","xde","zte"
        );
        $is_mobile=false;
        foreach($mobile_agents as $device){
    
    
            if(stristr($user_agent,$device)){
    
    
                $is_mobile=true;
                break;
            }
        }
        return $is_mobile;
    }
    // 协议法,因为有可能不准确,放到最后判断
    if (isset ($_SERVER['HTTP_ACCEPT'])) {
    
    
        // 如果只支持wml并且不支持html那一定是移动设备
        // 如果支持wml和html但是wml在html之前则是移动设备
        $is_mobile=false;
        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')))) {
    
    
            $is_mobile = true;
        }
        return $is_mobile;
    }
}
 /**
     * 判断是否手机
     */
    function is_mobile(){
    
    
        // returns true if one of the specified mobile browsers is detected
        // 如果监测到是指定的浏览器之一则返回true
        $regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";
        $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
        $regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
        $regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
        $regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
        $regex_match.=")/i";
        // preg_match()方法功能为匹配字符,既第二个参数所含字符是否包含第一个参数所含字符,包含则返回1既true
        return preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));
    }

猜你喜欢

转载自blog.csdn.net/xcbzsy/article/details/110187326
今日推荐