让梦冬眠
封藏纷乱的梦不牵挂
© JinYon.Net
All Rights Reserved.

php判断是否移动端并跳转到手机版

// 判断是否跳转到手机版(条件:是移动端、没有强制显示PC版、手机版没有关闭)
if (is_mobile() && $_COOKIE['client'] != 'pc') {
$content_url = str_replace(ROOT_URL, '', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
$dou->dou_header(ROOT_URL . M_PATH . '/' . $content_url);
}


/**
* +----------------------------------------------------------
* 判断是否是移动客户端
* +----------------------------------------------------------
*/
function is_mobile() {
static $is_mobile;
$user_agent = $_SERVER['HTTP_USER_AGENT'];

if (isset($is_mobile))
return $is_mobile;

if (empty($user_agent)) {
$is_mobile = false;
} else {
// 移动端UA关键字
$mobile_agents = array (
'Mobile',
'Android',
'Silk/',
'Kindle',
'BlackBerry',
'Opera Mini',
'Opera Mobi'
);
$is_mobile = false;

foreach ($mobile_agents as $device) {
if (strpos($user_agent, $device) !== false) {
$is_mobile = true;
break;
}
}
}

return $is_mobile;
}

 

2017-02-27
关键字: