php自定义函数,判定是否是使用手机浏览网站

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* 判断是否为手机浏览
*
* @access static
* @return bool
*/
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'])){
$keywords = 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',
);
if (preg_match("/(" . implode('|', $keywords) . ")/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;
}