使用阿里云CDN后,php使用$_SERVER['HTTP_VIA']判断是否是移动端会出错

使用阿里云CDN后,php使用$_SERVER['HTTP_VIA']判断是否是移动端会出错

if (isset ($_SERVER['HTTP_VIA'])) return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;

上面是之前代码,不用CDN不出错

但是用了CDN后,内容 var_dump($_SERVER['HTTP_VIA']) 会变为这样 string(54) "c********48.l2, cache29.l2cn2648" 

上面代码可以改为  if (isset ($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], 'wap')) return true;

function isMobile()
{
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) {
        return true;
    }

    if(isset ($_SERVER['HTTP_CLIENT']) &&'PhoneClient'==$_SERVER['HTTP_CLIENT']) return true;
    if (isset ($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], 'wap')) return true;
    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'
        );
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
            return true;
        }
    }
    if (isset ($_SERVER['HTTP_ACCEPT'])) {
        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')))) {
            return true;
        }
    }
    return false;
}

 

posted @ 2021-02-18 11:04  study_php_java_C++  阅读(678)  评论(0编辑  收藏  举报