A browser Check class

Source: Internet
Author: User
Tags strlen strtok version trim
Browser

A browser Check class


<?php
Original Author: epsilon7
// Sonymusic(sonymusic@163.net)
Class Browserdetector {
var $UA = ""; The contents of the $HTTP _user_agent
var $BROWSER = "Unknown"; Browser type
var $PLATFORM = "Unknown"; Operating system
var $VERSION = ""; Browser version number
var $MAJORVER = ""; Browser Major Version number
var $MINORVER = ""; Browser pair version number
/* Constructor started/*
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 program start
$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> ";
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.