<?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> ";
?>