Home > php教程 > php手册 > PHP判断客户端的浏览器类型

PHP判断客户端的浏览器类型

PHP中文网
Release: 2017-03-30 15:36:31
Original
2615 people have browsed it

 PHP判断客户端的浏览器类型

判断客户端浏览器类型

01    /**    
02     * 判断浏览器名称和版本    
03     */    
04    function get_user_browser()    
05    {    
06        if (empty($_SERVER['HTTP_USER_AGENT']))    
07        {    
08            return '';    
09        }    
10         
11        $agent       = $_SERVER['HTTP_USER_AGENT'];    
12        $browser     = '';    
13        $browser_ver = '';    
14         
15        if (preg_match('/MSIE\s([^\s;]+)/i', $agent, $regs))    
16        {    
17            $browser     = 'Internet Explorer';    
18            $browser_ver = $regs[1];    
19        }    
20        elseif (preg_match('/FireFox\/([^\s]+)/i', $agent, $regs))    
21        {    
22            $browser     = 'FireFox';    
23            $browser_ver = $regs[1];    
24        }    
25        elseif (preg_match('/Maxthon/i', $agent, $regs))    
26        {    
27            $browser     = '(Internet Explorer ' .$browser_ver. ') Maxthon';    
28            $browser_ver = '';    
29        }    
30        elseif (preg_match('/Opera[\s\/]([^\s]+)/i', $agent, $regs))    
31        {    
32            $browser     = 'Opera';    
33            $browser_ver = $regs[1];    
34        }    
35        elseif (preg_match('/OmniWeb\/(v*)([^\s;]+)/i', $agent, $regs))    
36        {    
37            $browser     = 'OmniWeb';    
38            $browser_ver = $regs[2];    
39        }    
40        elseif (preg_match('/Netscape([\d]*)\/([^\s]+)/i', $agent, $regs))    
41        {    
42            $browser     = 'Netscape';    
43            $browser_ver = $regs[2];    
44        }    
45        elseif (preg_match('/safari\/([^\s]+)/i', $agent, $regs))    
46        {    
47            $browser     = 'Safari';    
48            $browser_ver = $regs[1];    
49        }    
50        elseif (preg_match('/NetCaptor\s([^\s;]+)/i', $agent, $regs))    
51        {    
52            $browser     = '(Internet Explorer ' .$browser_ver. ') NetCaptor';    
53            $browser_ver = $regs[1];    
54        }    
55        elseif (preg_match('/Lynx\/([^\s]+)/i', $agent, $regs))    
56        {    
57            $browser     = 'Lynx';    
58            $browser_ver = $regs[1];    
59        }    
60         
61        if (!empty($browser))    
62        {    
63           return addslashes($browser . ' ' . $browser_ver);    
64        }    
65        else    
66        {    
67            return 'Unknow browser';    
68        }    
69    }
Copy after login

以上就是PHP判断客户端的浏览器类型的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template