<?php
if(!isMobile()){
echo "這是手機端";
}else{
echo "這是電腦端";
}
function isMobile() {
//如果有HTTP_X_WAP_PROFILE則一定是移動設備
if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])){
return true;
}
//如via信息有wap一定是移動設備
//部分服務商會屏蔽該信息
if (isset ($_SERVER['HTTP_VIA'])) {
//找不到為flase,否則為true
return stristr($_SERVER['HTTP_VIA'], "wap")
? true : false;
}
//判斷手機發送的客戶端標志,兼容性有待提高
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'
);
//從HTTP_USER_AGENT中查找手機瀏覽器的關鍵字
if(preg_match("/(".implode('|',$clientkeywords).")/i",
strtolower($_SERVER['HTTP_USER_AGENT']))) {
return true;
}
}
//協議法,因為有可能不准確,放到最后判斷
if (isset ($_SERVER['HTTP_ACCEPT'])) {
//如果只支持wml並且不支持html那一定是app
//如果支持wml和html但是wml在html之前則是app
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;
}
}
}
?>
文章來源:微筆記_博客