很多时候,在很多项目中都是用单入口访问,但是因为现在用移动设备的用户非常多,所以在做项目的时候,就得考虑移动设备的用户体验度,如果是安卓,IOS设备,则提供不同的返回页面,包括不同的浏览器判断,所以,小研究了一下,发现了不同浏览器在$_SERVER['HTTP_USER_AGENT']会有不同的结果判断,根据这个,我们就可以判断用户是持有什么设备访问的。
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
|
function
getBrowser(){
if
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'Maxthon'
)) {
$browser
=
'Maxthon'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'MSIE 12.0'
)) {
$browser
=
'IE12.0'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'MSIE 11.0'
)) {
$browser
=
'IE11.0'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'MSIE 10.0'
)) {
$browser
=
'IE10.0'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'MSIE 9.0'
)) {
$browser
=
'IE9.0'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'MSIE 8.0'
)) {
$browser
=
'IE8.0'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'MSIE 7.0'
)) {
$browser
=
'IE7.0'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'MSIE 6.0'
)) {
$browser
=
'IE6.0'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'NetCaptor'
)) {
$browser
=
'NetCaptor'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'Netscape'
)) {
$browser
=
'Netscape'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'Lynx'
)) {
$browser
=
'Lynx'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'Opera'
)) {
$browser
=
'Opera'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'Chrome'
)) {
$browser
=
'Google'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'Firefox'
)) {
$browser
=
'Firefox'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'Safari'
)) {
$browser
=
'Safari'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'iphone'
) ||
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'ipod'
)) {
$browser
=
'iphone'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'ipad'
)) {
$browser
=
'iphone'
;
}
elseif
(
strpos
(
$_SERVER
[
'HTTP_USER_AGENT'
],
'android'
)) {
$browser
=
'android'
;
}
else
{
$browser
=
'other'
;
}
return
$browser
;
}
|
之前有人加我QQ专门问过我这个问题,现在我公布出来,代码不一定要高深,但一定要经典。这段代码,可以无限扩展,而且至少可以开发出使用5-10年的项目了。好的代码都是简单的,实用的,易懂的。
本文转自 3147972 51CTO博客,原文链接:http://blog.51cto.com/a3147972/1334426,如需转载请自行联系原作者