Last updated May 27, 2006. Created by bermin on May 27, 2006.
Log in to edit this page.
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;
?>
Comments
Wrong
This code is wrong.
strpos returns 0 if string in 0-position and false if string not found.
Read PHP man
Wrong Code
I'm PHP illiterate (trying to learn, just hard-headed I guess). Anyway, could you post the corrected code please? Thanks!
Correct Code
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
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
Try this......
http://www.yoingco.com/BrowserCheckPHP.zip
Kisses
TraceyK
Another solution...
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
Opera
Thanks for the code, but I use Opera and it still outputs "Other Browsers."
Do you know if there is a problem with the code?
Take a look at this project
Take a look at this project http://drupal.org/project/browserclass
Drupal in the Amazon Jungle
WARNING!
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
--
Michael Bishop
Also having problems with caching
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
Same issue with caching
Thanks for sharing this! I had the same issue with caching causing seemingly erratic behaviour of my browser and OS detection script. Had to turn caching off to solve it.
People, what do you think
People, what do you think about
http://drupal.org/project/browscap
together with
http://drupal.org/project/switchtheme
Opera
Thanks for the code, but I use Opera and it still displays "Others Browsers." Could you check the code and make sure that it is corr