目前使用手機(jī)或平板瀏覽頁(yè)面越來(lái)越普遍,這就要有必要給自己網(wǎng)站做一個(gè)手機(jī)主題,但是如果要使用手機(jī)主題,就必須進(jìn)行判斷,判斷是否為手機(jī)用戶(hù),如果是,返回移動(dòng)端頁(yè)面,如果否則返回正常PC端頁(yè)面。下面這個(gè)PHP函數(shù),可以用來(lái)判斷客戶(hù)端類(lèi)型,從而確定輸出的頁(yè)面類(lèi)型。下面長(zhǎng)沙做網(wǎng)站湘龍工作室以wordpress做網(wǎng)站系統(tǒng)為例給大家講解下如何實(shí)現(xiàn)做網(wǎng)站系統(tǒng)自動(dòng)判斷訪客是否使用移動(dòng)設(shè)備。
第一步:自定義函數(shù)
在functions.php內(nèi)加入如下代碼
/*判斷是否移動(dòng)端訪問(wèn)*/
function is_mobile() {
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$mobile_browser = Array(
"mqqbrowser", //手機(jī)QQ瀏覽器
"opera mobi", //手機(jī)Opera
"juc","iuc",//UC瀏覽器
"fennec","ios","applewebKit/420","applewebkit/525","applewebkit/532","ipad","iphone","ipaq","ipod",
"iemobile", "windows ce",//windows phone
"240x320","480x640","acer","android","anywhereyougo.com","asus","audio","blackberry","blazer","coolpad" ,"dopod", "etouch", "hitachi","htc","huawei", "jbrowser", "lenovo","lg","lg-","lge-","lge", "mobi","moto","nokia","phone","samsung","sony","symbian","tablet","tianyu","wap","xda","xde","zte"
);
$is_mobile = false;
foreach ($mobile_browser as $device) {
if (stristr($user_agent, $device)) {
$is_mobile = true;
break;
}
}
return $is_mobile;
}
第二步:函數(shù)調(diào)用
條件判斷,在調(diào)用時(shí)返回不同的頁(yè)面內(nèi)容
<?php if (is_mobile() ): ?>
// 移動(dòng)端輸出內(nèi)容
<?php else: ?>
// PC端輸出內(nèi)容
<?php endif; ?>
其實(shí)上述代碼只要是php語(yǔ)言的網(wǎng)站都可以適用,如果是非wordpress系統(tǒng)的網(wǎng)站想要實(shí)現(xiàn)自動(dòng)判斷訪客是否使用移動(dòng)設(shè)備可以訪問(wèn)
學(xué)做網(wǎng)站論壇提問(wèn),我們有相關(guān)技術(shù)人員解答的。