pc

5.png

mobile

4.png

<?php

$clientKeyWords = ['mobile', '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'
];

function isMobile($clientKeyWords)
{
    if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) {
        return TRUE;
    }
    if (isset ($_SERVER['HTTP_VIA'])) {
        return stristr($_SERVER['HTTP_VIA'], "wap") ? TRUE : FALSE;
    }
    if (isset ($_SERVER['HTTP_USER_AGENT'])) {
        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;
}

if (isMobile($clientKeyWords)) {
    //手机模板
} else {
    //电脑模板
}

当然github上有更完善的类库
地址

 $detect = new \Mobile_Detect();
 if ($detect->isMobile()) {
               
            }

关注友儿不迷路

Last modification:September 19th, 2020 at 11:28 pm
如果觉得我的文章对你有用,请随意赞赏