方法如下
检查用户的agent字符串,它是浏览器发送的HTTP请求的一部分。用 $_SERVER['HTTP_USER_AGENT']
得到agent字符串信息。
比如:
php" id="highlighter_389801">
1
2
3
|
<?php echo $_SERVER [ 'HTTP_USER_AGENT' ]; ?> |
有可能是打印出这样的:
1
|
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) |
封装成函数:
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
|
function my_get_browser(){ if ( empty ( $_SERVER [ 'HTTP_USER_AGENT' ])){ return 'robot!' ; } if ( (false == strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'MSIE' )) && ( strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'Trident' )!==FALSE) ){ return 'Internet Explorer 11.0' ; } if (false!== strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'MSIE 10.0' )){ return 'Internet Explorer 10.0' ; } if (false!== strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'MSIE 9.0' )){ return 'Internet Explorer 9.0' ; } if (false!== strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'MSIE 8.0' )){ return 'Internet Explorer 8.0' ; } if (false!== strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'MSIE 7.0' )){ return 'Internet Explorer 7.0' ; } if (false!== strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'MSIE 6.0' )){ return 'Internet Explorer 6.0' ; } if (false!== strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'Edge' )){ return 'Edge' ; } if (false!== strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'Firefox' )){ return 'Firefox' ; } if (false!== strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'Chrome' )){ return 'Chrome' ; } if (false!== strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'Safari' )){ return 'Safari' ; } if (false!== strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'Opera' )){ return 'Opera' ; } if (false!== strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], '360SE' )){ return '360SE' ; } //微信浏览器 if (false!== strpos ( $_SERVER [ 'HTTP_USER_AGENT' ], 'MicroMessage' )){ return 'MicroMessage' ; }> } |
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。