欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP实现的浏览器检查类

程序员文章站 2024-04-02 09:21:40
本文实例讲述了php实现的浏览器检查类。分享给大家供大家参考,具体如下:

本文实例讲述了php实现的浏览器检查类。分享给大家供大家参考,具体如下:

<?php
//原作者:epsilon7
//sonymusic([email]sonymusic@163.net[/email])
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>";
?>

更多关于php相关内容感兴趣的读者可查看本站专题:《php文件操作总结》、《php运算与运算符用法总结》、《php网络编程技巧总结》、《php基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。