PHPBuilder - Browser Detector



RSS Twitter
Snippets Http

Browser Detector

by: Eric
|
April 19, 2006

Version: 1.0.6

Type: Class

Category: HTTP

License: GNU General Public License

Description: /****************************************** ** Description : BrowserDetector ** File Name : browserDetector.php3 ** Author : epsilon7 for PHyX8 studios ** Author Email : epsilon7@asphyxia.com ** Created : Thursday, May 11, 2000 ** Last Modified : Wednesday, July 12, 2000 ** Modified By : epsilon7 *' INFO: Returns Browser information based on HTTP_USER_AGENT Ported from The Webmonkey JavaScript Source ##BrowserDetector() ##Parses User-Agent string into useful info. ##Source: Webmonkey Code Library ##(http://www.hotwired.com/webmonkey/javascript/code_library/) ##Author: Rich Blaylock ##Author Email: blaylock@wired.com Usage: include("browserDetector.php3"); $bd = new BrowserDetector; Variables: $bd->BROWSER // Netscape, MSIE, Opera $bd->PLATFORM // Win95, Unix, Windows 98, Mac, PPC, etc. $bd->VERSION // MajorVersion.MinorVersion $bd->MAJORVER // Major Version (before . in version string) $bd->MINORVER // Minor Version (after . in version string) '****************************************/ Testing : Mozilla/4.73 [en] (Win98; U) Browser : Netscape Platform : Win98 Version : 4.73 Major Version : 4 Minor Version : 73



<?php
/******************************************
** Description   : BrowserDetector
** Version       : 1.0.6
** File Name     : browserDetector.php
** Author        : epsilon7 for PHyX8 studios
** Author Email  : epsilon7@asphyxia.com
** Created       : Thursday, May 11, 2000
** Last Modified : Thursday, April 20, 2006
** Modified By   : SecondV

INFO: Returns Browser information based on HTTP_USER_AGENT

Based on The Webmonkey JavaScript Source
##BrowserDetector()
##Parses User-Agent string into useful info.
##Source: Webmonkey Code Library
##(http://www.hotwired.com/webmonkey/javascript/code_library/)
##Author: Rich Blaylock
##Author Email: blaylock@wired.com

## CHANGE LOG

++Thursday, April 20, 2006
  Code cleanup is about it, not sure if this even works anymore.
  Will take another look later and see if it's worth..or needed..to rewrite
  it.
  
++Thursday, August 17, 2000
  Modified to recognize Netscape 6 Preview Releases

++Friday, August 4, 2000
  Modified to properly recognize iCab, an alernative browser for Macintosh
  (modified by suzerain -- http://www.suzerain.com/)

++Friday, July 28, 2000
  Opera browsers below version 4.0 should now be successfully detected.

++Thursday, July 20, 2000
  Replaced preg_match() with eregi()

++Wednesday, July 12, 2000
  Fixed PHP4 Bug

   Usage:
   include("browserDetector.php");
   $bd = new BrowserDetector;

   Variables:
   $bd->BROWSER         // Netscape, MSIE, Opera
   $bd->PLATFORM        // Win,Mac,Unix
   $bd->VERSION         // MajorVersion.MinorVersion
   $bd->MAJORVER        // Major Version (before . in version string)
   $bd->MINORVER        // Minor Version (after . in version string)

 '****************************************/
class BrowserDetector
{
  var $ua =  '';
  var $browser = 'Unknown';
  var $plateform = 'Unknown';
  var $version = '';
  var $majorver = '';
  var $minorver = '';

  /* START CONSTRUCTOR */

  function BrowserDetector()
  {
    $this->ua = $_SERVER['HTTP_USER_AGENT'];
    $preparens = '';
    $parens = '';
    $postparens = '';
    $i = strpos($this->ua, '(');

    if($i >= 0)
    {
      $preparens = trim(substr($this->ua, 0, $i));
      $parensTMP = substr($this->ua, $i + 1, strlen($this->ua));
      $j = strpos($parensTMP, ')');

      if($j >= 0)
      {
        $parens = substr($parensTMP, 0, $j);
        $postparens = trim(substr($parensTMP, $j + 1, strlen($parensTMP)));
      }
    }
    else
    {
      $preparens = $this->ua;
    }
    $browVer = $preparens;
    $token = trim(strtok($parens, ';'));

    while($token)
    {
      if($token == 'compatible')
      {
        //
      }
      elseif(eregi('MSIE', $token))
      {
        $browVer = $token;
      }
      elseif(eregi('Opera', $token))
      {
        $browVer = $token;
      }
      elseif(eregi('X11', $token) || eregi('SunOS', $token) || eregi('Linux', $token))
      {
        $this->platform = 'Unix';
      }
      elseif(eregi('Win', $token))
      {
        $this->platform = $token;
      }
      elseif(eregi('Mac', $token) || eregi('PPC', $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));
    }
    elseif(substr($browVer, 0, strlen('iCab')) == 'iCab')
    {
      $this->browser = 'iCab';
      $leftover = substr($browVer, strlen('iCab') + 1, strlen($browVer));
    }
    $leftover = trim($leftover);

    /* CHECK FOR OPERA BROWSERS BELOW VERSION 4.0 */
    if(!empty($postparens))
    {
      if(substr($postparens, 0, strlen('Opera')) == 'Opera')
      {
        $this->browser = 'Opera';
        $v = trim(substr($postparens, strlen('Opera') + 1, strlen($postparens)));
        $v = trim(substr($v, 0, strpos($v, ' ')));
        $leftover = $v;
      }
    }

    /* CHECK FOR NETSCAPE 6 PREVIEW RELEASES */
    if(!empty($postparens))
    {
      if(eregi('Netscape6', $postparens))
      {
        $this->browser = 'Netscape';
        $i = strpos($postparens, 'Netscape6');

        if($i >= 0)
        {
          $v = trim(substr($postparens, $i + strlen('Netscape6') + 1, strlen($postparens)));
          $leftover = $v;
        }
      }
    }
    $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;
    }
  }
}
?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Eric

Comment:



Comment:

(Maximum characters: 1200). You have characters left.