首页 >php教程 >php手册 >php获取用户浏览器版本代码

php获取用户浏览器版本代码

WBOY
WBOY原创
2016-05-25 16:38:431397浏览

在php中我们有个全局变量$_SERVER[\'HTTP_USER_AGENT\'];可以获取用户所有信息,我们要经过处理才可以判断用户浏览器是那种型的,下面的函数就可以准确的用户浏览器版本号代码.

function getbrowse() 
{ 
    $agent = $_server['http_user_agent']; 
    $browser = ''; 
    $browserver = ''; 
    $browser = array('lynx', 'mosaic', 'aol', 'opera', 'java', 'macweb', 'webexplorer', 'omniweb'); 
    for($i = 0; $i <= 7; $i ++){ 
        if(strpos($agent, $browsers[$i])){ 
            $browser = $browsers[$i]; 
            $browserver = &#39;&#39;; 
        } 
    } 
    if(ereg(&#39;mozilla&#39;, $agent) && !ereg(&#39;msie&#39;, $agent)){ 
        $temp = explode(&#39;(&#39;, $agent); 
        $part = $temp[0]; 
        $temp = explode(&#39;/&#39;, $part); 
        $browserver = $temp[1]; 
        $temp = explode(&#39; &#39;, $browserver); 
        $browserver = $temp[0]; 
        $browserver = preg_replace(&#39;/([d.]+)/&#39;, &#39;1&#39;, $browserver); 
        $browserver = $browserver; 
        $browser = &#39;netscape navigator&#39;; 
    } 
    if(ereg(&#39;mozilla&#39;, $agent) && ereg(&#39;opera&#39;, $agent)) { 
        $temp = explode(&#39;(&#39;, $agent); 
        $part = $temp[1]; 
        $temp = explode(&#39;)&#39;, $part); 
        $browserver = $temp[1]; 
        $temp = explode(&#39; &#39;, $browserver); 
        $browserver = $temp[2]; 
        $browserver = preg_replace(&#39;/([d.]+)/&#39;, &#39;1&#39;, $browserver); 
        $browserver = $browserver; 
        $browser = &#39;opera&#39;; 
    } 
    if(ereg(&#39;mozilla&#39;, $agent) && ereg(&#39;msie&#39;, $agent)){ 
        $temp = explode(&#39;(&#39;, $agent); 
        $part = $temp[1]; 
        $temp = explode(&#39;;&#39;, $part); 
        $part = $temp[1]; 
        $temp = explode(&#39; &#39;, $part); 
        $browserver = $temp[2]; 
        $browserver = preg_replace(&#39;/([d.]+)/&#39;,&#39;1&#39;,$browserver); 
        $browserver = $browserver; 
        $browser = &#39;internet explorer&#39;; 
    } 
    if($browser != &#39;&#39;){ 
        $browseinfo = $browser.&#39; &#39;.$browserver; 
    } else { 
        $browseinfo = false; 
    }
    return $browseinfo; 
} 
//应用方法 
//在ie中 
echo getbrowse(); //internet explorer 6.0 
//在firefox中 
echo getbrowse() ;//netscape navigator 5.0


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn