2

I have been using php's get_browser() function to do some light analytics of my users, mostly for debugging. However, it seems that the maintainer for browsercap.ini, the data that powers get_browser has quit the project, and while some folks seem to be working on restarting it, the data is getting very stale. They are talking about a total rewrite, and so I am afraid that it will take quite a while before it gets going again.

Does anybody know an alternative that does not rely on browsercap.ini?

Edit: I am sorry for the ambiguity in my question. I need this for debugging, not analytics. I am using it in a "report bug" tool in a web application that has a lot of cross-browser bugs, but I have no control over the client machines. It is a very old application (originally built for IE6) and we have been doing our best to bring it up to date, but the cross-browser issues are at times very hard to find.

Thank you again

Matt
  • 1,287
  • 2
  • 11
  • 25
  • It's still maintained (http://browscap.org/). But don't use `get_browser`. Better use a performant provider https://github.com/ThaDafinser/UserAgentParser – ThaDafinser Jan 14 '16 at 11:34

5 Answers5

6

This is the way I do it : I have included the most common browers out there.

<?php

function get_browsername() {
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE){
$browser = 'Microsoft Internet Explorer';
}elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== FALSE) {
$browser = 'Google Chrome';
}elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== FALSE) {
$browser = 'Mozilla Firefox';
}elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== FALSE) {
$browser = 'Opera';
}elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== FALSE) {
$browser = 'Apple Safari';
}else {
$browser = 'error'; //<-- Browser not found.
}
return $browser;
}

echo 'Browser is '. get_browsername(); //<-- Display the browser name

if(get_browsername() == 'Google Chrome') { 
// Use the function to check
}

Hope This helps.

rbtved
  • 76
  • 1
  • 2
0

I just took a look at Piwik, which is an Open Source alternative to Google Analytics, and they rely on $_SERVER['HTTP_USER_AGENT']; (documented here) but this is directly connected to get_browser (documented here). Also, remember that browsers can lie about their user agent, you should describe exactly what you are looking for.

emartel
  • 7,712
  • 1
  • 30
  • 58
0

https://github.com/ornicar/php-user-agent offers an alternative that is reasonably current (last update 5 months ago) if you just want to get information about visitors' browsers.

I have used it to lock IE out of our work intranet (cos I can).

DaveP
  • 568
  • 6
  • 18
0
function get_browsername($userAgent = $_SERVER['HTTP_USER_AGENT']){
    $browsers = array(
                    array("IE", "Microsoft Internet Explorer"),
                    array("Chrome", "Google Chrome"),
                    array("Firefox", "Mozzila Firefox"),
                    array("Opera", "Opera"),
                    array("Safari", "Apple Safari")
                );
    foreach($browsers as $browser){
        if (strpos($userAgent, $browser[0]) !== false)
        return $browser[1];
    }
    return "unknown";
}
0

i think you'd better use a parser lib to do this for you, i recommend ua-parser. this lib can parse the most of browsers you can meet, but the defect is also clear. this lib is too heavy...

Leooonard
  • 199
  • 1
  • 2