pc端和m端共用同一个域名时,php如何识别?

pc
mobile
代码语言:javascript
复制
<?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) . &#34;)/i&#34;, strtolower(_SERVER['HTTP_USER_AGENT']))) {
return TRUE;
}
}
if (isset ($_SERVER['HTTP_ACCEPT'])) {
if ((strpos(_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;vnd.wap.wml&#39;) !== FALSE) &amp;&amp; (strpos(_SERVER['HTTP_ACCEPT'], 'text/html') === FALSE || (strpos(_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;vnd.wap.wml&#39;) &lt; strpos(_SERVER['HTTP_ACCEPT'], 'text/html')))) {
return TRUE;
}
}
return FALSE;
}

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

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

代码语言:javascript
复制
 $detect = new \Mobile_Detect();
if ($detect->isMobile()) {

        }</code></pre></div></div>