Browser.php – Detecting a user’s browser from PHP

Browser.php – v1.6
download
browser-v1-6.zip – v1.6
release: 11/08/2009
md5: d0ccda266648c2b8ade37bcd996ff102
View typical usage examples
View Example Results
(view test file used in example)

Detecting the user’s browser type and version is helpful in web applications that harness some of the newer bleeding edge concepts. With the browser type and version you can notify users about challenges they may experience and suggest they upgrade before using such application. Not a great idea on a large scale public site; but on a private application this type of check can be helpful.

In an active project of mine we have a pretty graphically intensive and visually appealing user interface which leverages a lot of transparent PNG files. Because we all know how great IE6 supports PNG files it was necessary for us to tell our users the lack of power their browser has in a kind way.

Searching for a way to do this at the PHP layer and not at the client layer was more of a challenge than I would have guessed; the only script available was written by Gary White and Gary no longer maintains this script because of reliability. I do agree 100% with Gary about the readability; however, there are realistic reasons to desire the user’s browser and browser version and if your visitor is not echoing a false user agent we can take an educated guess.

I based this solution off of Gary White’s original solution but added a few things:

  • I added the ability to view the return values as class constants to increase the readability
  • Updated the version detection for Amaya
  • Updated the version detection for Firefox
  • Updated the version detection for Lynx
  • Updated the version detection for WebTV
  • Updated the version detection for NetPositive
  • Updated the version detection for IE
  • Updated the version detection for OmniWeb
  • Updated the version detection for iCab
  • Updated the version detection for Safari
  • Added detection for iPhone
  • Added detection for robots
  • Added detection for mobile devices
  • Added detection for BlackBerry
  • Updated Safari to remove mobile devices (iPhone)
  • Added detection for iPhone
  • Removed Netscape checks

This solution identifies the following Operating Systems:

  • Windows (Browser::PLATFORM_WINDOWS)
  • Windows CE (Browser::PLATFORM_WINDOWS_CE)
  • Apple (Browser::PLATFORM_APPLE)
  • Linux (Browser::PLATFORM_LINUX)
  • OS/2 (Browser::PLATFORM_OS2)
  • BeOS (Browser::PLATFORM_BEOS)
  • iPhone (Browser::PLATFORM_IPHONE)
  • iPod (Browser::PLATFORM_IPOD)
  • BlackBerry (Browser::PLATFORM_BLACKBERRY)

This solution identifies the following Browsers and does a best-guess on the version:

  • Opera (Browser::BROWSER_OPERA)
  • WebTV (Browser::BROWSER_WEBTV)
  • NetPositive (Browser::BROWSER_NETPOSITIVE)
  • Internet Explorer (Browser::BROWSER_IE)
  • Pocket Internet Explorer (Browser::BROWSER_POCKET_IE)
  • Galeon (Browser::BROWSER_GALEON)
  • Konqueror (Browser::BROWSER_KONQUEROR)
  • iCab (Browser::BROWSER_ICAB)
  • OmniWeb (Browser::BROWSER_OMNIWEB)
  • Phoenix (Browser::BROWSER_PHOENIX)
  • Firebird (Browser::BROWSER_FIREBIRD)
  • Firefox (Browser::BROWSER_FIREFOX)
  • Mozilla (Browser::BROWSER_MOZILLA)
  • Amaya (Browser::BROWSER_AMAYA)
  • Lynx (Browser::BROWSER_LYNX)
  • Safari (Browser::BROWSER_SAFARI)
  • iPhone (Browser::BROWSER_IPHONE)
  • iPod (Browser::BROWSER_IPOD)
  • Google’s Android(Browser::BROWSER_ANDROID)
  • Google’s Chrome(Browser::BROWSER_CHROME)
  • GoogleBot(Browser::BROWSER_GOOGLEBOT)
  • Yahoo!’s Slurp(Browser::BROWSER_SLURP)
  • W3C’s Validator(Browser::BROWSER_W3CVALIDATOR)
  • BlackBerry(Browser::BROWSER_BLACKBERRY)

Typical Usage:

$browser = new Browser();
if( $browser->getBrowser() == Browser::BROWSER_FIREFOX && $browser->getVersion() >= 2 ) {
	echo 'You have FireFox version 2 or greater';
}

12/9/2008 Update: removed an unused constant and renamed the constructor to use the PHP magic method __construct (thanks to Robin for locating the legacy constant and suggesting the use of the magic method).

2/19/2009 Update: updated typical usage to show a correct example! (thanks David!)

2/24/2009 Update: fixed typo in the usage! (thanks Adam!)

3/14/2009 Update: added support for the iPod; added iPod and iPhone as platforms; added Google’s Android

4/22/2009 Update: added support for GoogleBot, the W3C Validator and Yahoo! Slurp

4/27/2009 Update: John pointed out a terrible typo (see below) — removed the typo

11/08/2009 Update: A lot of changes to the script, thank you to everyone for the suggestions and emails. This release should add all of the requested features. Added BlackBerry, mobile detection, Opera Mini support, robot detection, Opera 10’s UserAgent “mess”, detection for IceCat and Shiretoko!

143 Comments so far »

  1. The all new PHP Browser Detection | mavrick said

    November 6 2008 @ 7:46 pm

    [...] PHP5 Browser Detection Download [...]

  2. Matthew Broome said

    November 7 2008 @ 5:54 am

    How do I embed this code into a html file?

  3. Chris Schuld said

    November 7 2008 @ 7:38 am

    Hey Matthew:

    The best way to embed this would be to include it from your HTML file (assuming your HTML is processed by PHP)

    < ?php

    require_once('Browser.php');
    $browser = new Browser();
    if( ! ( $browser->getBrowser() == Browser::BROWSER_FIREFOX && $browser->getVersion() >= 2 ) ) {
    echo ‘You have FireFox version 2 or greater’;
    }

    ?>

  4. Browser.php - version 1.1 - released! | Chris Schuld's Blog said

    November 8 2008 @ 12:37 am

    [...] updated the Browser.php class today to detect Google’s Chrome Browser. Additionally, thanks to an idea from Daniel [...]

  5. Amir said

    November 14 2008 @ 11:12 pm

    Hi,
    I’m trying to use your code but i get an error message which says “unexpecting “)”
    here’s the code i put in my header php (i use wordpress)

    if ($_GET["lab"]=="1") {
    require_once('browser_detect.php');
    $browser = new Browser();
    if ( $browser->getBrowser() == Browser::IE ) {
    $client_browser="_ie";
    }
    }

    issues might be with require_once() function in accessing the browser_detect.php, and I tried to put the file in my site root and also next to the header.php which calls the browser_detect.php!
    but i get the error all the time.
    Please help.

  6. Pablo Colla said

    November 15 2008 @ 8:01 am

    Come on! I get “You have Firefox….” message even if I am on IE.

  7. Chris Schuld said

    November 15 2008 @ 9:13 am

    @Amir:

    The unexpected “)” is likely in your wordpress file; you may want to put the Browser.php file in the same path as your template files and then call require_once like this require_once(dirname(__FILE__).’/Browser.php’);

    The Browser.php file is syntactically correct so the unexpected ‘)’ is probably in your WordPress source.

    @Pablo:

    Can you post the user agent string for your browser? The Browser.php file is tested against a large list of agents: http://chrisschuld.com/wp-content/uploads/2008/11/useragents.txt Any chance you can post your User Agent string so I can add it into the test file? If you don’t know your user agent you can browse here: http://chrisschuld.com/myuseragent.php

  8. Pablo Colla said

    November 17 2008 @ 12:29 am

    Hi Chris,

    This is the User Agent I get with IE:

    User Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.2)

  9. Damien said

    November 17 2008 @ 8:46 am

    Hi, i have include (with require) your class into a php page, but i receive this error:

    Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in ***************\inc\Browser.php on line 67

    (i have obscured the full url ;-) with ***)

    why?

    Many thanks.

  10. Chris Schuld said

    November 17 2008 @ 8:56 am

    @Damien

    Make sure you are using PHP5 for this class. If you are using PHP4 you should use Daniel’s PHP4 port located at: http://mavrick.id.au/programming/2008/the-all-new-php-browser-detection/

    Good Luck!

  11. Damien said

    November 19 2008 @ 10:51 am

    I understand. In fact now the web server where my site has the 4.2 version of php. Will be updated to php 5 by Dec. 15. Pending and then you can use an excellent class, I will see the link that gave me, thanking you for your kindness :-)

  12. Miguel Pereira said

    November 20 2008 @ 4:32 pm

    Hi !
    I’m a portuguese. So, sorry my bad english :P

    I try this script and not work with google chrome.

    it’s only me ? :S

    []

  13. Miguel Pereira said

    November 20 2008 @ 4:44 pm

    If you open http://apptools.com/phptools/browser/ with google chrome, you will see “safari”.

  14. Chris Schuld said

    November 20 2008 @ 10:58 pm

    @Miguel

    The site located at http://apptools.com/phptools/browser/ is NOT running this Browser.php class at all but rather the original work from Gary White which I updated. If you want to see the execution of this class you can click here: http://chrisschuld.com/proj/Browser/test.php

    This test will run the Browser class against this file: http://chrisschuld.com/proj/Browser/useragents.txt

  15. Greg said

    November 25 2008 @ 2:18 am

    Hey Chris,

    You mentioned an easy way to embed this into an HTML file above, is it possible to use that same concept so that the browser.php detects the browser/os/ect and then redirects members using, say, Opera and Netscape elsewhere? I know its probably a silly question, but I’m fairly new to php. :)

    Thanks in advance,
    -Greg

  16. Chris Schuld said

    November 25 2008 @ 4:29 pm

    @Greg

    No chance it is a silly question ;-) — but it does have a lot of answers, you could change the results depending on the browser you could also re-direct the user with an HTTP header redirect (301). Here is an example:

    $browser = new Browser();
    if( $browser->getBrowser() == Browser::IE ) {
    header(‘Location: /ie’);
    }
    else if( $browser->getBrowser() == Browser::FIREFOX ) {
    header(‘Location: /ff’);
    }
    else {
    header(‘Location: /other’);
    }

    If you want my advice try as hard as you can to write your pages to the standards. Generally, this will work great and you will only have to fight Internet Explorer.

  17. Michael Therrien said

    November 27 2008 @ 1:45 am

    Hello,
    I’m trying to do an include of this PHP page (with PHP) and it’s throwing this error:
    Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0

    Do you, or anyone else, know what’s causing this/why it is?

  18. Michael Therrien said

    November 27 2008 @ 1:49 am

    Well then, I guess it has nothing to do with this script. That’s odd. I never got this before…
    :P Guess I have more work to do!

  19. Chris Schuld said

    November 28 2008 @ 12:26 pm

    @Michael

    Michael, as you guessed this error (bug_compat_42) is caused by other code in your script. The bug_compat_42 error tells me you are probably trying to assign something to your _SESSION variable in the global scope; keep digging because it’s unrelated to the Browser.php file.

    Best of luck!

  20. Draugen said

    December 1 2008 @ 3:45 pm

    Very usefull code, saved me from a world of hazzle…
    Only one thing that puzzle me is the typical use example which to me seems to be a faulty if sentrence. Too me it looks like it will type out the Firefox message whenever you are not using the Firefox greater than 2.0 rather then when you are actually using it?

  21. robin said

    December 9 2008 @ 5:33 am

    Hi Chris,

    many thanks for your & Gary’s great work. I just wonder if it wouldn’t be nice to use __construct() as the constructor name instead of Browser() – just in case people have to rename your class to match their autoloading needs ;-)

  22. robin said

    December 9 2008 @ 6:16 am

    Just another question: What’s the reason for having both BROWSER_IE and BROWSER_INTERNET_EXPLORER? Seems to me that BROWSER_IE works, BROWSER_INTERNET_EXPLORER doesn’t, at least with IE7.

  23. Chris Schuld said

    December 9 2008 @ 7:42 am

    @robin

    Thank you for your suggestions; I removed the legacy constant BROWSER_INTERNET_EXPLORER (which was not used) and added the magic method suggestion and re-released Browser.php as v1.2. Thanks!

  24. Updated Browser.php to version 1.2 | Chris Schuld's Blog said

    December 9 2008 @ 7:45 am

    [...] browser detection project in php (Browser.php) has been updated to version 1.2 based on suggestions I received in the comments. The new version [...]

  25. Damien said

    December 17 2008 @ 9:23 am

    Hello dear mr Chris, I finally tested its excellent script and it works very well. In addition to thanking you, I wish them a question, perhaps not relevant: it is aware of a hybrid solution between php and javascript for the approval of the video resolution? I found several js script, but of course all suffer the problem of possible disabling js from the client user. Finally, I apologize for my not perfect English, are Italian.

  26. Violative said

    December 24 2008 @ 3:33 am

    I am getting…

    “Parse error: syntax error, unexpected ‘&’ in”

    I have tried in my html file

    &

    $browser = new Browser();
    if( ! ( $browser->getBrowser() == Browser::BROWSER_FIREFOX && $browser->getVersion() >= 2 ) ) {
    echo ‘You have FireFox version 2 or greater’;
    }

  27. Chris Schuld said

    December 24 2008 @ 9:10 am

    @Violative

    Can you leave some more details; the file is syntactically correct; likely the unexpected ‘&’ is coming from your HTML file in between your < ?php and ?> start/stop tags. Please post a few more details.

  28. Chris Schuld said

    December 24 2008 @ 9:13 am

    @Damien

    Since PHP executes at the server level with relatively no connection to the user’s environment there really is no way to ascertain the user’s display information (namely: resolution). You’ll need to use Javascript to grab the information.

  29. Violative said

    December 28 2008 @ 8:36 pm

    I will verify and retry.

  30. trixmasta said

    January 10 2009 @ 10:50 am

    Hi, thats an awsome script you have! The only problem is, im confused on how to get the browser name and put it in a variable…How do you do that?

    Thanks

  31. trixmasta said

    January 10 2009 @ 10:52 am

    Hi, how do you get the browser name, and put it in a variable?
    Thats the only part im confused about.

    Thanks

  32. Rick said

    February 16 2009 @ 1:35 pm

    I modified your browser.php script to add support for the browser in my T-Mobile G1 Phone (Android). Is this something that you’d like to roll into your version? If so, let me know and I can email you with the updated file.

  33. Chris Schuld said

    February 16 2009 @ 1:42 pm

    @Rick:

    Hi Rick, if you can send me the updated lines by email that would be helpful and I’ll apply it… thanks for the update/help.

    –Chris

  34. Rakibul Hasan Mitul said

    February 19 2009 @ 3:26 am

    Hi,
    I’m Mitul from bangladesh. i am a bigginer in php. i saw your code of browser.php. please tell me how can i see the output of this on my local browser?
    thank you.

  35. David said

    February 19 2009 @ 12:50 pm

    Hi Chris.
    I think the “Typical Usage” is wrong becauce of the “if( !(“.

  36. David said

    February 19 2009 @ 2:45 pm

    It would be great, if you could put back the netscape-browser check, because a little amount of people still using netscape.

  37. Chris Schuld said

    February 19 2009 @ 8:44 pm

    @David:

    Thank you David; you are 1000% correct; I had the bang (!) in there accidentally — that’ll teach me to cut-n-paste ;-)

    @Mitul:

    Hi Mitul, understood you are just starting out. Create a test.php page in the root of your web server and add the typical usage that David just helped me fix ;-) . The file should (a) include the Browser.php class and then have the typical usage in it… then look at your page with FireFox. I hope that helps!

  38. Max said

    February 20 2009 @ 4:27 pm

    Hello. I am a php noob… Let’s say I’m including Browser.php in another php file, to echo the user’s browser and system. I would use “require_once”, right? What would the echo command look like? I tried echo $browser which didn’t work. Sorry for being so dumb and thanks in advance!

  39. Adam said

    February 24 2009 @ 3:08 pm

    Thank you very much for this! It’s great! However, the “Typical Usage” is still wrong… “>= 2 ) )” has an extra closing parenthesis.

  40. Chris Schuld said

    February 24 2009 @ 3:51 pm

    @Adam: thanks; sorry for the typo

    @Max: no worries Max we were all rookies at some point…

    Here is what I would suggest:

     
    require_once('Browser.php'); // assuming Browser.php is in the same path as this script
     
    $browser = new Browser();
     
    if( $browser->getBrowser() == Browser::BROWSER_FIREFOX ) {
      echo "you have FireFox";
    }

    I hope that helps out!

  41. Almog Baku said

    March 1 2009 @ 5:30 am

    Thank you a lot!

    Almog Baku,
    Israel.

    P.S the class doesn’t detect Avant Browser..

  42. David said

    March 5 2009 @ 9:28 am

    Hi Chris,
    i have problems to detect the firefox version 3.0.6 and 3.0.7. He always shows me the version 3.0.5. Do you know where is a bug?

    thanks david

  43. Chris Schuld said

    March 5 2009 @ 9:38 am

    @David, can you post your User Agent string or visit: http://chrisschuld.com/proj/Browser/test.php

    I assume it is:

    Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (.NET CLR 3.5.30729)

    But would like to make sure!

    @Almog, I will look into the Avant browser; I had not heard of it until your comment; if you would like to contribute the check routine let me know.

  44. David said

    March 5 2009 @ 12:09 pm

    @Chris
    my User Agent is:

    Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.5, Ant.com Toolbar 1.2 (.NET CLR 3.5.30729).

    But I have installed the Version 3.0.7, before 3.0.6, it always tell me the 3.0.5 version. Maybe it the fault of the Ant.com Toolbar?!

  45. David said

    March 5 2009 @ 5:12 pm

    I found the problem.
    The Ant.com toolbar has fouled up my User Agent

    How to repair the User Agent
    1. Type “about:config” in the URL bar from Firefox and hit Enter
    2. Look for “general.useragent.extra.firefox”
    3. Right-click that preference and select Reset
    4. Restart Firefox – Done!

  46. Simon said

    March 8 2009 @ 7:04 am

    Hi Chris
    I’m a beginner at php i i was wandering:
    1. what code should i use in my hmtl page
    2. can i use this check to adjust a table’s height/width
    Thanks in advance

  47. Raymond said

    March 14 2009 @ 8:54 am

    Hi Chris,

    Good day!

    Could you by chance add “iPod” for devices to detect?

    Hoping for the implementation.

  48. Browser.php updated to v1.3 | Chris Schuld's Blog said

    March 14 2009 @ 9:28 am

    [...] on a comment on the Browser.php page I added support for the iPod today. I opted to place both the iPhone and iPod as browsers even [...]

  49. Chris Schuld said

    March 14 2009 @ 9:30 am

    @Raymond

    I added the iPod today as well as added both iPod and iPhone to the platform list.

  50. Episodes: Drupal integration & ingestor | Wim Leers said

    March 15 2009 @ 10:07 am

  51. Max said

    March 21 2009 @ 7:44 am

    Why do I get a bunch of **s when I do echo($browser->getBrowser());

    I am trying to just get the browsers name, but it gives me

    ** ** ** ** ** ** Firefox

    Thanks!

  52. Max said

    March 21 2009 @ 8:33 am

    Also, how do I get the version of the browser

  53. Max said

    March 21 2009 @ 8:48 am

    Nevermind. This is what I did to get what I wanted.

    require_once(‘Browser.php’);
    $browser = new Browser();
    $thebrowser = ereg_replace(“[^A-Za-z]“, “”, $browser->getBrowser());
    $ver = $browser->getVersion();

    echo “$thebrowser $ver”;

    Firefox 3.0 :)

  54. Enzo said

    March 28 2009 @ 8:09 pm

    Nice max, works like a charm :p

  55. Rich Nicholls said

    April 6 2009 @ 8:19 am

    Very nice. Just what I needed.

    Do you have any plans to add support for mail client user agents? I’m currently using this to get a list of what browsers the recipients from our mailing list are using when they load our emails, but mail clients such as Mac Mail or Blackberry are reported as ‘unknown’. Not exactly what this was intended for, but I’m just interested.

  56. Chris Schuld said

    April 6 2009 @ 8:24 am

    @Rich

    Do you happen to have the User Agents for the different devices you could share with me and I’ll add support in for them?

  57. AlexV said

    April 14 2009 @ 10:24 am

    It would be nice to detect major search engine crawlers (like Google Bot) and the W3C Validator bot too… This way when we validate our site or when our site is indexed, some content is fetched by the bot.

    Not really important, but would be a “nice to have” feature :)

    I found this link that might help for the W3C:
    http://www.useragentstring.com/pages/W3C-checklink/

    I also have a question… Is this script will continue to work with PHP 6 once released? Don’t see any reason why it shouldn’t, but just want to check…

  58. AlexV said

    April 14 2009 @ 10:25 am

  59. AlexV said

    April 14 2009 @ 1:30 pm

    IS there a way to detect Netscape versions? I know it’s dead, but my website still support it (7.2 and up)…

  60. Martin Pietschmann said

    April 18 2009 @ 9:17 am

    Hello Chris,

    I’ve implemented the OS identification.
    Maybe you want to include it in your version too.

    http://pastebin.com/f5657e881
    added line 122-154 and 195-199
    altered checkPlatform()

  61. Moises Zaragoza said

    April 20 2009 @ 7:40 am

    i just upload browser-v1-3.zip – v1.3 and i am getting

    Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in Browser.php on line 79

    my code is

    getBrowser() );
    ?>

  62. Federico said

    April 20 2009 @ 9:16 am

    For IE6 i used:
    getBrowser() == Browser::BROWSER_IE && $browser->getVersion()

  63. Justin said

    April 22 2009 @ 1:10 am

    Nice work man. Really good class. I’ve used it in some of my work and I’m very pleased with the results. I hope you continue to update the browsers :)

  64. Chris Schuld said

    April 22 2009 @ 9:53 am

    @Justin

    Thanks! Glad it helped you out! Before I rebuilt Gary’s solution I didn’t have a way to do this effectively either! I’m not a big fan of the PHP solution get_browser() either.

    @Moises

    Sounds like you are not using PHP5

    @AlexV

    I added GoogleBot and the W3C Validator in there. On the other hand, right now there are no plans to add in support for Netscape due to lack of demand; if you want to add them in and “patch” it to support Netscape let me know and I can put in your changes.

    Also, it will work with PHP6 whenever it is production ready.

  65. Browser.php updated to v1.4 | Chris Schuld's Blog said

    April 22 2009 @ 9:57 am

  66. John said

    April 26 2009 @ 9:01 am

    What does egeg(‘308|425|426|474|0b1′, $var) do on line 339? $var is also undefined.

  67. Browser.php updated to v1.5 | Chris Schuld's Blog said

    April 27 2009 @ 8:52 am

    [...] Typo updated and version 1.5 of Browser.php released: http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/ [...]

  68. Chris Schuld said

    April 27 2009 @ 8:53 am

    @John

    Thanks and great catch. I updated the source and released v1.5. Thanks again!

  69. Olivier said

    May 4 2009 @ 11:21 am

    Hi Chris, great script you have here. I was wondering if you were planning to add the os version detection, like distinguishing windows xp from vista

    The other script I found is this one, http://techpatterns.com/downloads/php_browser_detection.php , which does detect the platform version, but the whole script is very messy and the way to use it is not as practical as yours

    Anyway, I might give try at upgrading yours with this functuionnality if you don’t mind

  70. Nicholas Reed said

    May 22 2009 @ 6:20 am

    Exceptional, I’m going to use it right now for Spargos.net.au

  71. Bachelor thesis finished! | Wim Leers said

    May 22 2009 @ 6:56 am

    [...] Browser.php [...]

  72. Jean-Jacques said

    May 23 2009 @ 9:27 am

    Hi there,

    How would I implement if I want the detection to result in routing the user to another page specifically, based on the specific browser?

    I’m a noob obviously.

    Thanks!

  73. Trong Hiep Le said

    May 26 2009 @ 1:13 am

    Great job man ^_^

  74. cristi mihai said

    May 27 2009 @ 11:48 am

    my best regards man . great works. thanks

  75. Daniel Lang said

    May 28 2009 @ 5:37 pm

    Hi,

    v1.5 has been ported to PHP4. Check it out here: http://mavrick.id.au/programming/2008/the-all-new-php-browser-detection/

  76. Chris Schuld said

    May 28 2009 @ 5:44 pm

    @Daniel Lang

    Thanks Daniel!

  77. Saidur Rahman said

    June 4 2009 @ 4:15 am

    Very nice article.

    Thanks
    Rana

  78. Tim said

    June 5 2009 @ 3:38 am

    I’m now using Daniel Lang’s version for PHP4, but still getting the same errors I had when using the PHP5 version.

    Using different bits of code from this page give different errors, for example:

    require_once(“Browser.php”);
    $browser = new Browser();
    if( $browser->getBrowser() == Browser::BROWSER_FIREFOX && $browser->getVersion() >= 2 ) {
    echo ‘You have FireFox version 2 or greater’;
    }

    gives

    Fatal error: Undefined class constant ‘BROWSER_FIREFOX’ in /splash.php on line 7

    Any ideas?

  79. Daniel Lang said

    June 9 2009 @ 11:47 pm

    @Tim

    If you are using the PHP4 version the ‘::’ before the constant is not supported in PHP4; only for a method I believe, try this:

    if( ( $browser->getBrowser() == $browser->BROWSER_FIREFOX && $browser->getVersion() >= 2 ) ) {
    echo ‘You have FireFox version 2 or greater’;
    }

    - or -

    if( ( $browser->getBrowser() == ‘firefox’ && $browser->getVersion() >= 2 ) ) {
    echo ‘You have FireFox version 2 or greater’;
    }

    - or -

    if( ( $browser->isBrowser(‘firefox’) && $browser->getVersion() >= 2 ) ) {
    echo ‘You have FireFox version 2 or greater’;
    }

  80. Nayan said

    June 12 2009 @ 3:14 am

    Thanks a lot,
    Your class file is very helpfull.

    I am so happy to get it.

  81. callan winfield said

    June 13 2009 @ 9:21 pm

    brilliant, thanks for this. very easy to implement.

  82. Topher said

    June 16 2009 @ 5:23 pm

    Thanks for the code. For BlackBerry:

    protected function checkBrowserBlackBerry() {
      $retval = false;
      if( eregi('BlackBerry',$this->_agent) ) {
         $aresult = explode("/",stristr($this->_agent,"BlackBerry"));
         $aversion = explode(' ',$aresult[1]);
         $this->setVersion($aversion[0]);
         $this->_browser_name = self::BROWSER_BLACKBERRY;
         $retval = true;
       }
       return $retval;
    }

    Enhancement suggestion is to add isMobile property. It would make it easy to know when to optimize for small real estate screen, spotty javascript support, etc.

    Thanks for the hard work,
    TC

  83. Rob said

    June 18 2009 @ 1:59 pm

    Greetings:

    I had been using Gary’s original script for a while and it always worked fine I seem to be having an issue detecting the difference between IE 7.x and IE 8.x. Have you seen this or do you know of a reliable way to tell the difference?

    $browser->Version

    still returns 7.0 using 8.0.

    Thanks in advance and thanks for the updates to the script!
    -Rob

  84. Daniel Lang said

    June 19 2009 @ 5:03 am

    @Rob

    Did you check to make sure IE8 wasn’t running in compatibility mode for IE7 ?

  85. Rob said

    June 19 2009 @ 5:09 am

    Yes. I have not turned that on yet.

  86. Rob said

    June 19 2009 @ 5:17 am

    Right after I said that I checked again. It seems the default is to “Display all websites in Compatibility View”. Once I unchecked this it shows correctly as 8.0. Interestingly enough, when it compatibility mode the rendering was completely different then when rendered in the actual 7.0 version. .

    Thanks for the tip Daniel, and thanks again for the updated script Chris and all who have added components.

    -Rob

  87. hi said

    June 22 2009 @ 6:17 am

    hi i have this code
    curl_setopt($ch, CURLOPT_USERAGENT, “Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10″);

    and this part of the code
    “User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1\r\n” .
    i would like to change this code to IE5 OR IE6

    can you help me this will help the script that i am running suess above display but the flash player doesnt play the flv on any IE but it works great with firefox i wonder why.

  88. Daniel Lang said

    June 24 2009 @ 8:19 pm

    @hi

    Goto http://chrisschuld.com/proj/Browser/useragents.txt to get a list of known User Agents, you should be able to test out a few different 5.x and 6.x User Agents for IE.

    Not sure exactly what you are trying to achieve here however remember to remove the \r\n at the end of the User Agent string you supplied. If you still have issues with flash try posting on a Flash related website or update your Flash Player plugin for IE.

  89. Ken said

    June 27 2009 @ 11:40 am

    Really well put together – thanks

  90. Nugraha said

    June 28 2009 @ 9:41 pm

    huy chriss

    i got a problem with opera mini in phone
    i use this code

    $browser = new Browser();
    $thebrowser = ereg_replace(“[^A-Za-z]“, “”, $browser->getBrowser());
    $ver = $browser->getVersion();

    echo “”;
    echo “$thebrowser $ver”;

    but this script detect opera version 9.60
    actually i use opera mini version 4.1

    please help me to correct this

  91. Daniel Lang said

    July 7 2009 @ 8:49 pm

    @Nugraha

    Can you supply the User Agent string from your phone?

  92. Nick said

    July 8 2009 @ 8:45 am

    Great script, does exactly what I need. Thanks for spending the time to put it together.

  93. AlexV said

    July 9 2009 @ 8:06 am

    Chris, did you received my last two email with my added features to your class?

    Thanks!

  94. Adam said

    July 13 2009 @ 9:18 am

    Is there any plan to update this for PHP 5.3.0?

  95. Patrice said

    July 20 2009 @ 6:33 pm

    Chris,

    When you get a chance, can you please add support for Iceweasel browser (http://en.wikipedia.org/wiki/GNU_IceCat). It’s actually Firefox but your class does not recognize as it.

    Thanks!

  96. Jeff Flesher said

    July 26 2009 @ 3:17 pm

    Add isRobot function
    Add to Constructor
    /* The most common ones. The rest alphabetically. */
    $this->_robots = array(‘Googlebot’, ‘msnbot’, ‘Slurp’, ‘Yahoo’,'Arachnoidea’, ‘ArchitextSpider’, ‘Ask Jeeves’, ‘B-l-i-t-z-Bot’, ‘Baiduspider’, ‘BecomeBot’, ‘cfetch’, ‘ConveraCrawler’, ‘ExtractorPro’, ‘FAST-WebCrawler’, ‘FDSE robot’, ‘fido’, ‘geckobot’, ‘Gigabot’, ‘Girafabot’, ‘grub-client’, ‘Gulliver’, ‘HTTrack’, ‘ia_archiver’, ‘InfoSeek’, ‘kinjabot’, ‘KIT-Fireball’, ‘larbin’, ‘LEIA’, ‘lmspider’, ‘Lycos_Spider’, ‘Mediapartners-Google’, ‘MuscatFerret’, ‘NaverBot’, ‘OmniExplorer_Bot’, ‘polybot’, ‘Pompos’, ‘Scooter’, ‘Teoma’, ‘TheSuBot’, ‘TurnitinBot’, ‘Ultraseek’, ‘ViolaBot’, ‘webbandit’, ‘www.almaden.ibm.com/cs/crawler’, ‘ZyBorg’,);

    Add function
    function isRobot()
    {
    foreach($this->_robots as $robot)
    {
    if(strpos($this->_agent, $robot) !== false)
    {
    return true;
    }
    }
    return false;
    }

  97. Ville said

    August 2 2009 @ 7:24 pm

    Use of eregi() is causing warnings with PHP 5.3.0.. an update would be much appreciated!!

  98. Chris said

    August 6 2009 @ 8:26 am

    I got it working fine with Google Chrome but it took a little bit of working out how to do it. Did you come back with the update?

  99. Arnold said

    August 12 2009 @ 1:18 am

    Chris,

    Are you planning to update the script i.e. for Chrome and other linux distro’s like ubuntu etc? A lot has changed since April :-)

  100. Manuel Brotz said

    August 15 2009 @ 9:36 am

    Hi

    I use Shiretoko under Ubuntu 9.04.
    This is Firefox 3.5 under Ubuntu.

    Change the function checkBrowserFirefox():

        protected function checkBrowserFirefox() {
          $retval = false;
          if( eregi('Firefox',$this->_agent) ) {
            $aresult = explode('/',stristr($this->_agent,'Firefox'));
            $aversion = explode(' ',$aresult[1]);
            $this->setVersion($aversion[0]);
            $this->setBrowser(self::BROWSER_FIREFOX);
            $retval = true;
          } elseif( eregi('Shiretoko',$this->_agent) ) {
            $aresult = explode('/',stristr($this->_agent,'Shiretoko'));
            $aversion = explode(' ',$aresult[1]);
            $this->setVersion($aversion[0]);
            $this->setBrowser(self::BROWSER_FIREFOX);
            $retval = true;
          }
          return $retval;
        }
    

    It works for me.

    Greetz M. Brotz

  101. Andrew Mellenger said

    August 26 2009 @ 9:34 am

    Would this work for BlackBerries? I’m having trouble with jQuery working on a site I built and would like to just exclude all javascript if a BB browser is detected. thoughts?

  102. Reiborn said

    September 7 2009 @ 7:43 am

    Use of eregi() is causing warnings with PHP 5.3.0.. an update would be much appreciated!!

  103. Juozas said

    September 11 2009 @ 2:39 am

    The script doesn’t detect my Opers browser correctly. It’s version 10.00 while it shows earlier version. Code i’m using:
    getBrowser().” “.$br->getVersion().” OS: “.$br->GetPlatform();
    // full code at http://juozas24.puslapiai.lt/source.php?file=blah.php
    ?>

    My user agent: Opera/9.80 (Windows NT 5.1; U; lt) Presto/2.2.15 Version/10.00

  104. Juozas said

    September 11 2009 @ 2:41 am

    My prev post a bit messed up… That’s real Code:

    require_once “browser.php”;
    $br = new Browser;
    ecgo “Browser: “.$br->getBrowser().” “.$br->getVersion().” OS: “.$br->GetPlatform();
    // full code at http://juozas24.puslapiai.lt/source.php?file=blah.php

  105. Sanjay said

    September 21 2009 @ 11:23 pm

    Hi,
    Since eregi is deprecated in php 5.3, could you replace eregi with suitable regex

  106. Chris Schuld said

    September 21 2009 @ 11:26 pm

    @ALL

    I am testing and have a new version of Browser.php coming that supports PHP5.3. Sorry all; I have been busy converting a few other projects to 5.3 … an update is near!

  107. Paris Paraskevas said

    September 23 2009 @ 1:42 am

    thanks for the heads up chris,

    mainly all occurences of eregi need to be replaced with preg_match

    just updated my local test server to 5.3 and i get all sorts of errors :) commented out any libraries that are not essiential and will try and update them all later on, hopefully will have a fresh copy of yours in a few days

  108. Daniel Lang said

    September 25 2009 @ 5:48 am

    @Chris

    Any Idea if Google Chrome Frame will alter the USERAGENT? http://code.google.com/chrome/chromeframe/

  109. Chris Schuld said

    September 25 2009 @ 6:06 am

    @Daniel

    Hey Daniel! Hope you are well! From what I can tell so far (as of the latest version) Google Frame uses the IE6 USERAGENT on the server side but adds “googleframe” to it. (side note: I am going to add it into Browser.php) The client side sees the browser as Google Chrome ironically.

    I have the PHP5.3 version ready with bunch of updates — I will add this in as well. Just need to find some time to get it online!

  110. paris @ united worx web design said

    September 25 2009 @ 6:32 am

    chromeframe will be used as i have read for Google Wave, they have decided to focus on safari,firefox,chrome and leave out IE since it would take a lot of development time. chromeframe should render a page using chrome rendering engine and therefore should identify itself as Google chrome.

    thanks for getting a PHP 5.3 version ready chris, will sure grab a copy once its online.

  111. Daniel Lang said

    September 28 2009 @ 10:06 pm

    @Chris

    Glad to see you’re still advancing on the Browser Detection script, I look forward to porting it back to PHP4.x :D Did a little reading up on Google Frame and didn’t hesitate to install it :) Shame it doesn’t seem to work on IE8 Win7 64bit!

    @Paris

    From what I understand the whole idea of Google Frame was to make IE6,7,8 play ball with web standards and actually support standard html tags properly?

    Anyways, would be nice to detect if a user has it installed or not :)

  112. Daniel Lang said

    September 28 2009 @ 10:16 pm

    Windows XP SP3 Build 2600 – IE v7.0.5730.13

    Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6; chromeframe; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)

  113. William from Lagos said

    September 29 2009 @ 5:28 pm

    Script doesnt track the latest version of Opera properly. Returns a different version number rather than 10.0

  114. Than said

    October 12 2009 @ 9:56 pm

    I tried to use Browser.php on my website, which is hosted on Unix Operating System, it does not work. any idea? the following is the code:

    getBrowser() == $browser->BROWSER_FIREFOX && $browser->getVersion() <= 4 )
    {
    echo "”;
    }
    elseif( $browser->getBrowser() == $browser->BROWSER_OPERA && $browser->getVersion() <= 10 )
    {
    echo "”;
    }
    elseif( $browser->getBrowser() == $browser->BROWSER_IE && $browser->getVersion() >= 8 )
    {
    echo “”;
    }
    elseif( $browser->getBrowser() == $browser->BROWSER_IE && $browser->getVersion() == 7 )
    {
    echo “”;
    }
    elseif( $browser->getBrowser() == $browser->BROWSER_IE && $browser->getVersion() == 6 )
    {
    echo “”;
    }
    elseif( $browser->getBrowser() == $browser->BROWSER_SAFARI && $browser->getVersion() <= 5 )
    {
    echo "”;
    }
    elseif( $browser->getBrowser() == $browser->BROWSER_CHROME && $browser->getVersion() <= 4 )
    {
    echo "”;
    }
    else
    {
    echo “”;
    }
    ?>

    thanks

  115. Than said

    October 12 2009 @ 9:58 pm

    Sorry here the code again…

    getBrowser() == $browser->BROWSER_FIREFOX && $browser->getVersion() <= 4 )
    {
    echo "”;
    }
    elseif( $browser->getBrowser() == $browser->BROWSER_OPERA && $browser->getVersion() <= 10 )
    {
    echo "”;
    }
    elseif( $browser->getBrowser() == $browser->BROWSER_IE && $browser->getVersion() >= 8 )
    {
    echo “”;
    }
    else( $browser->getBrowser() == $browser->BROWSER_IE && $browser->getVersion() == 7 )
    {
    echo “”;
    }

    ?>

  116. basiL said

    October 15 2009 @ 4:36 am

    functions eregi() and ereg_replace() have been removed from PHP 5.3 !!!!!!

  117. Nick said

    October 17 2009 @ 7:37 pm

    You forgot blackberry
    FAIL

  118. windguard said

    October 28 2009 @ 12:31 pm

    Hey Chris, thanks for this amazing script.

    The only problem I seem to be having with it is when I upload it on the server, it gives me this error:

    Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in C:\Inetpub\vhosts\l2-standard.com\httpdocs\_common\_php\browser-detection.php on line 86

    Am I doing something wrong or is there something wrong with my host? And is there any way this can be solved?

    Thanks, windguard

  119. Adam said

    November 3 2009 @ 6:41 pm

    Hi Chris,

    I know you’re busy, but do you have an estimate as to when you might have the new version online? I would love to have the update and I’m sure everybody else would too :)

  120. Mark said

    November 6 2009 @ 10:20 pm

    Hey!
    Just found this forum, Chris, please inform me when you have your script up… I’m so frustrated, each browser shows a site different… !

  121. Opera 10’s User Agent | Chris Schuld's Blog said

    November 8 2009 @ 10:11 am

    [...] 10’s user agent, the Opera team has provide some “fun” for all of us. In my Browser project I started getting feedback that it was broken. At the 10,000′ level, it was defintely broken [...]

  122. Chris Schuld said

    November 8 2009 @ 10:58 am

    Hello Everyone,

    Thank you for your emails about the Browser.php project. I am SO SORRY for the extended delay; all of my time has been eaten by work! I just posted the latest version of 1.6 for you! Here are a few notes, kudos and comments!

    @Daniel Lang – Daniel: thank you for your continued help!

    @Saidur, @Nayan, @Callan Winfield, @Ken, @Nick: Thank You — glad you enjoyed the Script

    @Topher: thank you for the code snippet for BlackBerry, I updated the calls for PHP5.3 and added it in!

    @Alex: Alex, Netscape Navigator and it’s odd re-versioning are included in this version as well

    @Jeff: I’ll try to add all of the crawlers you provided; thanks!

    @Nugraha: Opera Mini is now in version 1.6+ (thanks!)

    @Ville, @Reiborn, @Sanjay: thanks, PHP5.3 version has been in the works for a while, here it is finally!

    @Manuel: thanks for the code, I added full support for Shiretoko

    @Andrew Mellenger: it now has BlackBerry support, thanks

    @Juozas, @William: agreed, Opera 10 has a unique User Agent story, but this version (1.6+) fixes this problem

    @Nick: grow up

    @Adam and @Mark — thanks for the final push ;-)

    The 1.6 release adds support for the following:

    PHP 5.3, Opera Mini, BlackBerry (as a platform and as a browser), IceCat, Shiretoko, isRobot(), isMobile(), support the deprecated Netscape Navigator version 9, Opera version 10, and additional documentation

  123. Browser.php updated to v1.6 | Chris Schuld's Blog said

    November 8 2009 @ 11:06 am

    [...] Version 1.6 of Browser.php has been released with a lot of updates: http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/ [...]

  124. Daniel Lang said

    November 9 2009 @ 4:57 am

  125. Daniel Lang said

    November 11 2009 @ 4:53 am

    oOooOo haven’t seen this one yet: Mozilla/5.0 (compatible; Google Desktop)

  126. Ron said

    November 11 2009 @ 1:14 pm

    Here is a suggestion, detect if they have a certain .NET version.
    If I send the function “3.5.0.0″ then return true or false if they at least have that version installed.

    Example,
    http://msdn.microsoft.com/en-us/library/bb909885.aspx

  127. Jason Antman’s Blog » Android links - maps, dial a phone number said

    November 22 2009 @ 8:31 am

    [...] know that there are a number of PHP classes out there to detect browsers (like Chris Schuld’s browser.php) and some things to detect mobile device capabilities (like WURFL or Tera WURFL, both using the [...]

  128. JavieR said

    November 22 2009 @ 8:39 pm

    Nice work with this script. I was using the original script by Gary White but that one doesn’t work with PHP 5.3

    @Daniel Lang: Thanks for the PHP4 version too!

  129. juozas said

    November 23 2009 @ 3:30 am

    Thanks for update. Now the script detects browser correctly. :D

  130. David said

    November 25 2009 @ 6:42 pm

    Hi Chris,

    I would be interested to hear your thoughts on handsetdetection.com if you get the time..

    David

  131. PHP探路者 said

    November 26 2009 @ 10:53 pm

    [...] PHP5 Browser v1.6 Detection Page [...]

  132. Daniel Lang said

    November 30 2009 @ 6:47 pm

    @David

    Seems dodgy… just my $0.02

    We’re looking at including a better mobile base detection for this script, be sure to check back for more information at a later date :)

  133. khurram shizad said

    December 22 2009 @ 8:26 am

    Hi!

    That is really a great effort to help us.
    Thanks.

    regards,
    khurram shahzad

  134. Ollie said

    December 30 2009 @ 7:30 pm

    Hi Chris,

    Looking at your script, it is very nice, but might I make a small suggestion? There is a massive usage of preg_match, where something like stripos would do? Is there any reason for this? – stripos being usually a far faster solution, anyone looking at saving processing power in big scripts should consider this!

    example:

    if( preg_match(‘/blackberry/i’,$this->_agent) )

    could be:

    if(! stripos($this->_agent, ‘blackberry’) === false )

    it achieves the same effect, and my execute faster, but does take a little extra typing!

    Just a thought.

    Regards,
    Ollie

  135. Chris Schuld said

    January 4 2010 @ 11:44 pm

    @Ollie

    I completely agree; all of the preg_match syntax is legacy from the previous content owner. I 100% agree it needs to be fixed… just an equation of time to dedicate to it (including testing). Next time I rev it I’ll remove some of them! Soon it will be fixed

    Thanks Ollie!

  136. Alex10336 said

    February 2 2010 @ 7:26 am

    Iceweasel support (full open source firefox)
    [php]
    /**
    * Determine if the browser is Firefox or not
    * @return boolean True if the browser is Firefox otherwise false
    */
    protected function checkBrowserFirefox() {
    $retval = false;
    if( preg_match(‘/Firefox/i’,$this->_agent) ) {
    $aresult = explode(‘/’,stristr($this->_agent,’Firefox’));
    $aversion = explode(‘ ‘,$aresult[1]);
    $this->setVersion($aversion[0]);
    $this->setBrowser(self::BROWSER_FIREFOX);
    $retval = true;
    }
    if( preg_match(‘/Iceweasel/i’,$this->_agent) ) {
    $aresult = explode(‘/’,stristr($this->_agent,’Iceweasel’));
    $aversion = explode(‘ ‘,$aresult[1]);
    $this->setVersion($aversion[0]);
    $this->setBrowser(self::BROWSER_FIREFOX);
    $retval = true;
    }
    return $retval;
    }
    [/php]

  137. Adam said

    February 17 2010 @ 2:06 pm

    Version 1.6 is great! However, I had the following message come up in my error log a few times:

    PHP Notice: Undefined offset: 1 in [path]\Browser.php on line 664

    This is the line:

    $aversion = explode(‘ ‘,$aresult[1]);

    This is the context of the line:

    protected function checkBrowserFirefox() {
    $retval = false;
    if( preg_match(‘/Firefox/i’,$this->_agent) ) {
    $aresult = explode(‘/’,stristr($this->_agent,’Firefox’));
    $aversion = explode(‘ ‘,$aresult[1]);
    $this->setVersion($aversion[0]);
    $this->setBrowser(self::BROWSER_FIREFOX);
    $retval = true;
    }
    return $retval;
    }

  138. Ben Dover said

    February 18 2010 @ 11:20 am

    Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Avant Browser; Avant
    Browser; .NETCLR 2.0.50727) probably MSIE version 6.0 running on Windows

    That’s Avant

  139. Santiago said

    February 18 2010 @ 2:10 pm

    Chrome Frame add the string chromeframe to the agent of IE. It will be great if you add this to the class.
    Thanks

  140. Binish Philip said

    February 24 2010 @ 11:26 pm

    Hi Chris,

    Thanks a lot for this amazing script.

    :Binish Philip

  141. maolunar said

    February 25 2010 @ 11:32 pm

    great job, dude!

  142. Kelly Watts said

    March 2 2010 @ 9:47 am

    Thank you for this. It represents a ton of work that you have done and is very useful.

  143. Basar said

    March 4 2010 @ 7:04 am

    very usefull script. thank you very much. thank you so so so much :)

Comment RSS · TrackBack URI

Leave a comment

Name: (Required)

Email: (Required)

Website:

Comment: