Browser sniffer in PHP code

The advantage of making a browser sniffer in PHP code is that it can be contained entirely in your HTML file.

Example:

<?
function SniffBrowser() {

   /***************************************************************************
   This function is a browser sniffer that returns a string-indexed array.
   The call:
      $browser = SniffBrowser();
   will return the following:
   $browser['type'] = Netscape, Explorer, Opera, Amaya, Unknown
   $browser['version'] = version number (float)
   $browser['platform'] = Windows, Mac, Other
   $browser['css'] = CSS version
   $browser['dom'] = DOM type: NS, IE, W3C, 0
   ***************************************************************************/

   global $HTTP_USER_AGENT;
   $b = $HTTP_USER_AGENT; $vers = 0.0;

   // detect browser brand and version number
   if (eregi('Opera[ \/]([0-9\.]+)' , $b, $a)) {
      $type = 'Opera';}
   elseif (eregi('Netscape[[:alnum:]]*[ \/]([0-9\.]+)', $b, $a)) {
      $type = 'Netscape';}
   elseif (eregi('MSIE[ \/]([0-9\.]+)', $b, $a)) {
      $type = 'Explorer';}
   elseif (eregi('Mozilla[ \/]([0-9\.]+)' , $b, $a)) {
      if (eregi('compatible' , $b)) {
         $type = 'Unknown';}
      else {
         $type = 'Netscape';}}
   elseif (eregi('([[:alnum:]]+)[ \/v]*([0-9\.]+)' , $b, $a)) {
      $type = $a[1]; $vers = $a[2];}
   else {
      $type = 'Unknown';}
   if (!$vers) $vers = $a[1];
   $browser['type'] = $type;
   $browser['version'] = $vers;

   // detect platform
   if (eregi('Win',$b)) $browser['platform'] = 'Windows';
   elseif (eregi('Mac',$b)) $browser['platform'] = 'Mac';
   else $browser['platform'] = 'Other';

   // find CSS version
   // note: it is unknown which future versions will support CSS2
   if ($type == 'Netscape' && $vers >= 4 ||
   $type == 'Explorer' && $vers >= 3 ||
   $type == 'Opera' && $vers >= 3) {
      $browser['css'] = 1;
      if ($type == 'Netscape' && $vers >= 5 ||
      $type == 'Explorer' && $vers >= 6 ||
      $type == 'Opera' && $vers >= 4) {
         $browser['css'] = 2;}}

   // detect DOM version
   $browser['dom'] = '0';
   if ($type == 'Explorer' && $vers >= 4 && $browser['platform'] == 'Windows') {
      // note: it is unknown which DOM model future versions of Explorer will use
      $browser['dom'] = 'IE';}
   elseif ($type == 'Opera' && $vers >= 5) {
      $browser['dom'] = 'W3C';}
   elseif ($type == 'Netscape') {
      if ($vers >= 5) $browser['dom'] = 'W3C';
      elseif ($vers >= 4) $browser['dom'] = 'NS';}

   // return array of answers
   return $browser;}

$browser = SniffBrowser();
?>

<? if ($browser['type'] == 'Netscape') { ?>
   <BLINK>Important message</BLINK>
<? } elseif ($browser['type'] == 'Explorer') { ?>
   <MARQUEE>Important message</MARQUEE>
<? } else { /* browser is not Explorer or Netscape */ ?>
   <FONT COLOR='#FF0000' SIZE=+1>Important message</FONT>
<? } ?>
<p>

<?
echo "Browsertype: " . $browser['type'] . "<br>\n";
echo "Version: " . $browser['version'] . "<br>\n";
echo "Platform: " . $browser['platform'] . "<br>\n";
echo "CSS version: " . $browser['css'] . "<br>\n";
echo "DOM version: " . $browser['dom'] . <br>\n";
?>
</p>
      

Explanation:

This code is intended to display the text 'Important message' in a way that attracts attention. In Netscape it produces the HTML tag <BLINK> which is only supported in Netscape. In Explorer it produces the HTML tag <MARQUEE> which is only supported in Explorer. In other browsers it writes the text in red.

The function SniffBrowser() returns a string-indexed array containing all the information we may need about the browser.

Note, that we can switch back and forth between PHP code and HTML code, even inside a branch.

The result is shown here:

Important message

Browsertype: CCBot
Version: 1.0
Platform: Other
CSS version:
DOM version: 0

See the animations page for another example.

This page was last modified 2008-Dec-08