首页 > 开发 > PHP > 正文

一个浏览器检查类

2024-05-04 22:55:13
字体:
来源:转载
供稿:网友

一个浏览器检查类


<?php
//原作者:epsilon7
//sonymusic([email protected])
class browserdetector  {
    var $ua =  "";    //$http_user_agent的内容
    var $browser=  "unknown";    //浏览器类型
    var $platform   =  "unknown";    //操作系统
    var $version=  "";    //浏览器版本号
    var $majorver   =  "";    //浏览器主版本号
    var $minorver   =  "";    //浏览器副版本号
    /* 构造函数开始 */
    function browserdetector(){  
        $this->ua   =  getenv(http_user_agent);   
        $preparens  =  "";   
        $parens =  "";   
        $i = strpos($this->ua,"(");   
        if ($i >= 0)   {  
            $preparens  = trim(substr($this->ua,0,$i));  
            $parens = substr($this->ua,$i+1,strlen($this->ua));  
            $j = strpos($parens,")");  
            if($j>=0)  {  
                $parens = substr($parens,0,$j);  
            }   
        }   
        else   {  
            $preparens = $this->ua;   
        }   
        $browver =  $preparens;   
        $token  =  trim(strtok($parens,";"));   
        while($token)   {  
            if($token=="compatible")  {
            }  
            elseif(preg_match("/msie/i","$token"))  {  
                $browver = $token;  
            }  
            elseif(preg_match("/opera/i","$token"))  {  
                $browver = $token;  
            }  
            elseif(preg_match("/x11/i","$token") || preg_match("/sunos/i","$token") || preg_match ("/linux/i","$token"))  {  
                $this->platform   =  "unix";  
            }  
            elseif(preg_match("/win/i","$token"))  {  
                $this->platform   =  $token;  
            }  
            elseif(preg_match("/mac/i","$token") || preg_match("/ppc/i","$token"))  {  
                $this->platform   =  $token;  
            }  
            $token = strtok(";");   
        }   
        $msieindex  =  strpos($browver,"msie");   
        if($msieindex >= 0)   {  
            $browver =  substr($browver,$msieindex,strlen($browver));   
        }   
        $leftover   =  "";   
        if(substr($browver,0,strlen("mozilla")) == "mozilla")   {  
            $this->browser =  "netscape";  
            $leftover=substr($browver,strlen("mozilla")+1,strlen($browver));   
        }   
        elseif(substr($browver,0,strlen("lynx")) == "lynx")   {  
            $this->browser =  "lynx";  
            $leftover=substr($browver,strlen("lynx")+1,strlen($browver));   
        }   
        elseif(substr($browver,0,strlen("msie")) == "msie") {  
            $this->browser =  "ie";  
            $leftover=substr($browver,strlen("msie")+1,strlen($browver));   
        }   
        elseif(substr($browver,0,strlen("microsoft internet explorer")) == "microsoft internet  explorer")   {  
            $this->browser =  "ie";  
            $leftover=substr($browver,strlen("microsoft internet explorer")+1,strlen($browver));   
        }  
        elseif(substr($browver,0,strlen("opera")) == "opera")   {  
            $this->browser =  "opera";  
            $leftover=substr($browver,strlen("opera")+1,strlen($browver));   
        }   
        $leftover = trim($leftover);   
        $i=strpos($leftover," ");   
        if($i > 0)   {  
            $this->version = substr($leftover,0,$i);   
        }   
        else   {  
            $this->version = $leftover;   
        }   
        $j = strpos($this->version,".");   
        if($j >= 0)   {  
            $this->majorver = substr($this->version,0,$j);  
            $this->minorver = substr($this->version,$j+1,strlen($this->version));   
        }   
        else   {  
            $this->majorver = $this->version;   
        }
    }
}

//测试程序开始
$test=new browserdetector;
echo $test->ua."<br>";
echo $test->browser."<br>";
echo $test->platform."<br>";
echo $test->version."<br>";
echo $test->majorver."<br>";
echo $test->minorver."<br>";
?>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表