Detect browser and display

Last modified: May 27, 2006 - 22:36

Want to detect the browser the user is using? This should help.

<?php
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') )
{
   if (
strpos($_SERVER['HTTP_USER_AGENT'], 'Netscape') )
   {
    
$browser = 'Netscape (Gecko/Netscape)';
   }
   else if (
strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') )
   {
    
$browser = 'Mozilla Firefox (Gecko/Firefox)';
   }
   else
   {
    
$browser = 'Mozilla (Gecko/Mozilla)';
   }
}
else if (
strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') )
{
   if (
strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') )
   {
    
$browser = 'Opera (MSIE/Opera/Compatible)';
   }
   else
   {
    
$browser = 'Internet Explorer (MSIE/Compatible)';
   }
}
else
{
  
$browser = 'Others browsers';
}

echo
$browser;
?>

Wrong

OnkelTem - July 13, 2008 - 12:27

This code is wrong.
strpos returns 0 if string in 0-position and false if string not found.
Read PHP man

Wrong Code

sysipheantaskmaster - August 10, 2008 - 22:53

I'm PHP illiterate (trying to learn, just hard-headed I guess). Anyway, could you post the corrected code please? Thanks!

Correct Code

Solenoid - August 26, 2008 - 08:31

The code is actually correct.

OnkelTem would be correct if what was being searched for were at the start of $_SERVER['HTTP_USER_AGENT'] but it is not in this case.

If you are going to use this to do your own searches on strings where the search term could exist at the start of the string, strpos() could return 0, which equates to false. If you change all of the conditions to:
if (strpos(...) === true)
then it could be used for searching on any strings. And yes, there is meant to be three equal signs. Read up on php.net about strpos and Booleans if you are still confused.

A php function that you might also find useful is the get_browser function (http://au.php.net/manual/en/function.get-browser.php)

After outputting

Solenoid - August 26, 2008 - 09:12

After outputting $_SERVER['HTTP_USER_AGENT'] in my browsers for my own testing, I found that Opera does actually put it's name at the start (but also does not include 'MSIE'), meaning that this code is actually incorrect. Here is the correct code (including a search for Safari)

<?php
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') )
{
  
$browser = 'Safari';
}
else if (
strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') )
{
   if (
strpos($_SERVER['HTTP_USER_AGENT'], 'Netscape') )
   {
    
$browser = 'Netscape (Gecko/Netscape)';
   }
   else if (
strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') )
   {
    
$browser = 'Mozilla Firefox (Gecko/Firefox)';
   }
   else
   {
    
$browser = 'Mozilla (Gecko/Mozilla)';
   }
}
else if (
strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') )
{
  
$browser = 'Internet Explorer (MSIE/Compatible)';
}
else if (
strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') === true)
{
  
$browser = 'Opera';
}
else
{
  
$browser = 'Other browsers';
}

echo
$browser;
?>

Try

TraceyK - August 30, 2008 - 15:03

Try this......

http://www.yoingco.com/BrowserCheckPHP.zip

Kisses

TraceyK

Another solution...

cbschuld - November 4, 2008 - 21:58

I was struggling to locate a robust solution so I created one.... available here: http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-fr...

--Chris

WARNING!

ablemike - December 6, 2008 - 02:58

We launched a site with a browser check outputing a string in the body, and when we turned on cache on the live site, it started caching the user agent from other people.
We had the browser checking script in a php file in our theme and when cache turned on, it started serving the script funky. Not sure how to solve this issue. Will report it back when I find out how to do this effectively.

--
Michael Bishop

Also having problems with caching

nickbn - December 30, 2009 - 19:04

I'm also having problems with mixing caching and browser detection in my theme. Basically, I detect browser display a block with a message that the user's browser sucks (IE6). With caching on it's display is inconsistent. Any thoughts? It doesn't appear to be "re-detecting" the browser once the page is cached so everyone gets the cached page with or without the block displayed.

Thanks!

Nick Buck-Niehaus
nick@nickbn.com

 
 

Drupal is a registered trademark of Dries Buytaert.