本文实例讲述了PHP简单判断手机设备的方法。分享给大家供大家参考,具体如下:
现在移动互联网越来越发到,很多的网站都普及了手机端浏览,为了更好的让网页在手机端显示,我们都选择了使用CSS媒体查询制作响应式模版,但这也有弊端,例如某些网站的结构是CMS类型的,太多的内容要显示,而使用CSS媒体查询设计响应式,只会隐藏但还是加载了,为了让手机端更快速的显示出内容,我们可以使用这个PHP判断手机设备代码,使用这个代码可以很方便的显示或不显示自定义的内容。
这是PHP判断手机设备函数代码,复制到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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
<?phpfunction is_mobile() { $user_agent = $_SERVER ['HTTP_USER_AGENT']; $mobile_browser = Array ( "mqqbrowser", // 手机QQ浏览器 "opera mobi", // 手机opera "juc", "iuc", // uc浏览器 "fennec", "ios", "applewebKit/420", "applewebkit/525", "applewebkit/532", "ipad", "iphone", "ipaq", "ipod", "iemobile", "windows ce", // windows phone "240×320", "480×640", "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;}?>//这是调用代码,可以加上if判断:<?php if(is_mobile()):?>//设置手机端的内容<?php endif; ?> |
|
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php$agent = strtolower($_SERVER['HTTP_USER_AGENT']);$iphone = (strpos($agent, 'iphone')) ? true : false;$ipad = (strpos($agent, 'ipad')) ? true : false;$android = (strpos($agent, 'android')) ? true : false;if($iphone || $ipad){ echo "<script>window.location.href='pc.html'</script>";//这里也可以是网址}if($android){ echo "<script>window.location.href='andorid.html'</script>";//这里也可以是网址}?> |