9

I tried to search in google but cannot find a complete solution (i only find something detects only the browser's type like firefox, opera) .

i want a php class or code to check the user's Browser including the version and also the operating system.

Thanks

CodeOverload
  • 47,274
  • 54
  • 131
  • 219

4 Answers4

22

a simple way for example:

function browser() {
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $browser = 'chromium';
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $browser = 'chrome';
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $browser = 'safari';
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $browser = 'opera';
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $browser = 'msie';
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $browser = 'mozilla';

    preg_match('/('.$browser.')[ \/]([\w]+)/', $ua, $version);

    return array($browser,$version[2], 'name'=>$browser,'version'=>$version[2]);
}

its return like

chromium 15
chrome 16
opera 9
goker
  • 2,690
  • 22
  • 20
  • Seems to work for me with IE 10. Wont work for IE 11 though (see [here](http://www.nczonline.net/blog/2013/07/02/internet-explorer-11-dont-call-me-ie/)) but that isn't a major issue for me. – zelanix Nov 12 '14 at 14:34
11

I used the techpatterns.com one and they don't always update it and it use procedural code which feel dated...

The Wolfcast BrowserDetection PHP class is updated and use an Object-Oriented way to do it:

You use it this way:

$browser = new BrowserDetection();
echo 'You are using ', $browser->getBrowser(), ' version ', $browser->getVersion();

Another example:

$browser = new BrowserDetection();
if ($browser->getBrowser() == BrowserDetection::BROWSER_FIREFOX && $browser->compareVersions($browser->getVersion(), '5.0.1') !== 1) {
    echo 'You have FireFox version 5.0.1 or greater. ';
}
AlexV
  • 22,658
  • 18
  • 85
  • 122
  • Thanks alex, that's much better :) – CodeOverload Jan 26 '10 at 19:52
  • 1
    I use the techpatterns.com one, have for over a decade. It's far more accurate than most. It gets updated all the time, not sure what that person saw or believes, in fact, it just added blink support, re rendering engines. It's designed to be very fast, and it is, procedural code isn't 'outdated', it's procedural code, there's no overhead of loading then creating an object, it's just a matter of which serves the purpose better. The output options of this one are quite extensive. Top comment header explains them. Very very robust and very flexible, and very powerful. – Lizardx Dec 24 '15 at 23:31
6

PHP actually has a native method for detecting browser info, called get-browser

Directly copied from PHP documentation:

<?php
echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";

$browser = get_browser(null, true);
print_r($browser);
?>

The above example will output something similar to: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3

Array
(
    [browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
    [browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*
    [parent] => Firefox 0.9
    [platform] => WinXP
    [browser] => Firefox
    [version] => 0.9
    [majorver] => 0
    [minorver] => 9
    [cssversion] => 2
    [frames] => 1
    [iframes] => 1
    [tables] => 1
    [cookies] => 1
    [backgroundsounds] =>
    [vbscript] =>
    [javascript] => 1
    [javaapplets] => 1
    [activexcontrols] =>
    [cdf] =>
    [aol] =>
    [beta] => 1
    [win16] =>
    [crawler] =>
    [stripper] =>
    [wap] =>
    [netclr] =>
)

Jay Zeng
  • 1,423
  • 10
  • 10
  • 7
    But you didn't copied the note: **In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system.** – machineaddict Jul 30 '13 at 13:11
1

get_browser() gives you browser version and operating system

$browser = get_browser();

foreach ($browser as $name => $value) {
    echo "$name $value\n";
}

output:
browser_name_pattern:</b> Mozilla/4\.5.*
parent: Netscape 4.0
platform: Linux
...
Mohammed Joraid
  • 6,202
  • 2
  • 27
  • 38
pedro
  • 411
  • 3
  • 9