Any PHP Code to Detect the Browser with Version and Operating System

Any php code to detect the browser with version and operating system?

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. ';
}

Get operating system info

The code below could explain in its own right, how http://thismachine.info/ is able to show which operating system someone is using.

What it does is that, it sniffs your core operating system model, for example windows nt 5.1 as my own.

It then passes windows nt 5.1/i to Windows XP as the operating system.

Using: '/windows nt 5.1/i' => 'Windows XP', from an array.

You could say guesswork, or an approximation yet nonetheless pretty much bang on.

Borrowed from an answer on SO https://stackoverflow.com/a/15497878/

<?php

$user_agent = $_SERVER['HTTP_USER_AGENT'];

function getOS() {

global $user_agent;

$os_platform = "Unknown OS Platform";

$os_array = array(
'/windows nt 10/i' => 'Windows 10',
'/windows nt 6.3/i' => 'Windows 8.1',
'/windows nt 6.2/i' => 'Windows 8',
'/windows nt 6.1/i' => 'Windows 7',
'/windows nt 6.0/i' => 'Windows Vista',
'/windows nt 5.2/i' => 'Windows Server 2003/XP x64',
'/windows nt 5.1/i' => 'Windows XP',
'/windows xp/i' => 'Windows XP',
'/windows nt 5.0/i' => 'Windows 2000',
'/windows me/i' => 'Windows ME',
'/win98/i' => 'Windows 98',
'/win95/i' => 'Windows 95',
'/win16/i' => 'Windows 3.11',
'/macintosh|mac os x/i' => 'Mac OS X',
'/mac_powerpc/i' => 'Mac OS 9',
'/linux/i' => 'Linux',
'/ubuntu/i' => 'Ubuntu',
'/iphone/i' => 'iPhone',
'/ipod/i' => 'iPod',
'/ipad/i' => 'iPad',
'/android/i' => 'Android',
'/blackberry/i' => 'BlackBerry',
'/webos/i' => 'Mobile'
);

foreach ($os_array as $regex => $value)
if (preg_match($regex, $user_agent))
$os_platform = $value;

return $os_platform;
}

function getBrowser() {

global $user_agent;

$browser = "Unknown Browser";

$browser_array = array(
'/msie/i' => 'Internet Explorer',
'/firefox/i' => 'Firefox',
'/safari/i' => 'Safari',
'/chrome/i' => 'Chrome',
'/edge/i' => 'Edge',
'/opera/i' => 'Opera',
'/netscape/i' => 'Netscape',
'/maxthon/i' => 'Maxthon',
'/konqueror/i' => 'Konqueror',
'/mobile/i' => 'Handheld Browser'
);

foreach ($browser_array as $regex => $value)
if (preg_match($regex, $user_agent))
$browser = $value;

return $browser;
}

$user_os = getOS();
$user_browser = getBrowser();

$device_details = "<strong>Browser: </strong>".$user_browser."<br /><strong>Operating System: </strong>".$user_os."";

print_r($device_details);

echo("<br /><br /><br />".$_SERVER['HTTP_USER_AGENT']."");

?>

Footnotes:
(Jan. 19/14) There was a suggested edit on Jan. 18, 2014 to add /msie|trident/i by YJSoft a new member on SO.

The comment read as:

Comment: because msie11's ua doesn't include msie (it includes trident instead)

I researched this for a bit, and found a few links explaining the Trident string.

  • http://www.sitepoint.com/ie11-smells-like-firefox/
  • http://www.upsdell.ca/BrowserNews/res_sniff.htm
  • How can I target only Internet Explorer 11 with JavaScript?
  • http://en.wikipedia.org/wiki/Trident_%28layout_engine%29
  • https://stackoverflow.com/a/17907562/1415724
  • http://msdn.microsoft.com/en-us/library/ie/bg182625(v=vs.110).aspx
  • An article on MSDN Blogs
  • An article on NCZOnline

Although the edit was rejected (not by myself, but by some of the other editors), it's worth reading up on the links above, and to use your proper judgement.


As per a question asked about detecting SUSE, have found this piece of code at the following URL:

  • http://codes-sources.commentcamarche.net/source/49533-operating-system-detection

Additional code:

/* return Operating System */
function operating_system_detection(){
if ( isset( $_SERVER ) ) {
$agent = $_SERVER['HTTP_USER_AGENT'];
}
else {
global $HTTP_SERVER_VARS;
if ( isset( $HTTP_SERVER_VARS ) ) {
$agent = $HTTP_SERVER_VARS['HTTP_USER_AGENT'];
}
else {
global $HTTP_USER_AGENT;
$agent = $HTTP_USER_AGENT;
}
}
$ros[] = array('Windows XP', 'Windows XP');
$ros[] = array('Windows NT 5.1|Windows NT5.1)', 'Windows XP');
$ros[] = array('Windows 2000', 'Windows 2000');
$ros[] = array('Windows NT 5.0', 'Windows 2000');
$ros[] = array('Windows NT 4.0|WinNT4.0', 'Windows NT');
$ros[] = array('Windows NT 5.2', 'Windows Server 2003');
$ros[] = array('Windows NT 6.0', 'Windows Vista');
$ros[] = array('Windows NT 7.0', 'Windows 7');
$ros[] = array('Windows CE', 'Windows CE');
$ros[] = array('(media center pc).([0-9]{1,2}\.[0-9]{1,2})', 'Windows Media Center');
$ros[] = array('(win)([0-9]{1,2}\.[0-9x]{1,2})', 'Windows');
$ros[] = array('(win)([0-9]{2})', 'Windows');
$ros[] = array('(windows)([0-9x]{2})', 'Windows');
// Doesn't seem like these are necessary...not totally sure though..
//$ros[] = array('(winnt)([0-9]{1,2}\.[0-9]{1,2}){0,1}', 'Windows NT');
//$ros[] = array('(windows nt)(([0-9]{1,2}\.[0-9]{1,2}){0,1})', 'Windows NT'); // fix by bg
$ros[] = array('Windows ME', 'Windows ME');
$ros[] = array('Win 9x 4.90', 'Windows ME');
$ros[] = array('Windows 98|Win98', 'Windows 98');
$ros[] = array('Windows 95', 'Windows 95');
$ros[] = array('(windows)([0-9]{1,2}\.[0-9]{1,2})', 'Windows');
$ros[] = array('win32', 'Windows');
$ros[] = array('(java)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2})', 'Java');
$ros[] = array('(Solaris)([0-9]{1,2}\.[0-9x]{1,2}){0,1}', 'Solaris');
$ros[] = array('dos x86', 'DOS');
$ros[] = array('unix', 'Unix');
$ros[] = array('Mac OS X', 'Mac OS X');
$ros[] = array('Mac_PowerPC', 'Macintosh PowerPC');
$ros[] = array('(mac|Macintosh)', 'Mac OS');
$ros[] = array('(sunos)([0-9]{1,2}\.[0-9]{1,2}){0,1}', 'SunOS');
$ros[] = array('(beos)([0-9]{1,2}\.[0-9]{1,2}){0,1}', 'BeOS');
$ros[] = array('(risc os)([0-9]{1,2}\.[0-9]{1,2})', 'RISC OS');
$ros[] = array('os/2', 'OS/2');
$ros[] = array('freebsd', 'FreeBSD');
$ros[] = array('openbsd', 'OpenBSD');
$ros[] = array('netbsd', 'NetBSD');
$ros[] = array('irix', 'IRIX');
$ros[] = array('plan9', 'Plan9');
$ros[] = array('osf', 'OSF');
$ros[] = array('aix', 'AIX');
$ros[] = array('GNU Hurd', 'GNU Hurd');
$ros[] = array('(fedora)', 'Linux - Fedora');
$ros[] = array('(kubuntu)', 'Linux - Kubuntu');
$ros[] = array('(ubuntu)', 'Linux - Ubuntu');
$ros[] = array('(debian)', 'Linux - Debian');
$ros[] = array('(CentOS)', 'Linux - CentOS');
$ros[] = array('(Mandriva).([0-9]{1,3}(\.[0-9]{1,3})?(\.[0-9]{1,3})?)', 'Linux - Mandriva');
$ros[] = array('(SUSE).([0-9]{1,3}(\.[0-9]{1,3})?(\.[0-9]{1,3})?)', 'Linux - SUSE');
$ros[] = array('(Dropline)', 'Linux - Slackware (Dropline GNOME)');
$ros[] = array('(ASPLinux)', 'Linux - ASPLinux');
$ros[] = array('(Red Hat)', 'Linux - Red Hat');
// Loads of Linux machines will be detected as unix.
// Actually, all of the linux machines I've checked have the 'X11' in the User Agent.
//$ros[] = array('X11', 'Unix');
$ros[] = array('(linux)', 'Linux');
$ros[] = array('(amigaos)([0-9]{1,2}\.[0-9]{1,2})', 'AmigaOS');
$ros[] = array('amiga-aweb', 'AmigaOS');
$ros[] = array('amiga', 'Amiga');
$ros[] = array('AvantGo', 'PalmOS');
//$ros[] = array('(Linux)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3}(rel\.[0-9]{1,2}){0,1}-([0-9]{1,2}) i([0-9]{1})86){1}', 'Linux');
//$ros[] = array('(Linux)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3}(rel\.[0-9]{1,2}){0,1} i([0-9]{1}86)){1}', 'Linux');
//$ros[] = array('(Linux)([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3}(rel\.[0-9]{1,2}){0,1})', 'Linux');
$ros[] = array('[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3}', 'Linux');
$ros[] = array('(webtv)/([0-9]{1,2}\.[0-9]{1,2})', 'WebTV');
$ros[] = array('Dreamcast', 'Dreamcast OS');
$ros[] = array('GetRight', 'Windows');
$ros[] = array('go!zilla', 'Windows');
$ros[] = array('gozilla', 'Windows');
$ros[] = array('gulliver', 'Windows');
$ros[] = array('ia archiver', 'Windows');
$ros[] = array('NetPositive', 'Windows');
$ros[] = array('mass downloader', 'Windows');
$ros[] = array('microsoft', 'Windows');
$ros[] = array('offline explorer', 'Windows');
$ros[] = array('teleport', 'Windows');
$ros[] = array('web downloader', 'Windows');
$ros[] = array('webcapture', 'Windows');
$ros[] = array('webcollage', 'Windows');
$ros[] = array('webcopier', 'Windows');
$ros[] = array('webstripper', 'Windows');
$ros[] = array('webzip', 'Windows');
$ros[] = array('wget', 'Windows');
$ros[] = array('Java', 'Unknown');
$ros[] = array('flashget', 'Windows');
// delete next line if the script show not the right OS
//$ros[] = array('(PHP)/([0-9]{1,2}.[0-9]{1,2})', 'PHP');
$ros[] = array('MS FrontPage', 'Windows');
$ros[] = array('(msproxy)/([0-9]{1,2}.[0-9]{1,2})', 'Windows');
$ros[] = array('(msie)([0-9]{1,2}.[0-9]{1,2})', 'Windows');
$ros[] = array('libwww-perl', 'Unix');
$ros[] = array('UP.Browser', 'Windows CE');
$ros[] = array('NetAnts', 'Windows');
$file = count ( $ros );
$os = '';
for ( $n=0 ; $n<$file ; $n++ ){
if ( preg_match('/'.$ros[$n][0].'/i' , $agent, $name)){
$os = @$ros[$n][1].' '.@$name[2];
break;
}
}
return trim ( $os );
}

Edit: April 12, 2015

I noticed a question yesterday that could be relevant to this Q&A and may be helpful for some. In regards to:

Mozilla/5.0 (Linux; Android 4.4.2; SAMSUNG-GT-I9505 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36

  • Question: Store specific data in variable from another variable with regex with PHP
  • Answer: https://stackoverflow.com/a/29584014/

Another edit, and adding a reference link that was asked (and answered/accepted today, Nov. 4/16) which may be of use.

Consult the Q&A here on Stack:

  • PHP Regex for OS detection

Detect exact OS version from browser

Short answer: You can't.

Long answer:

All you have is the information in the HTTP User-Agent header, which usually contains the OS name and version.

Usually, browsers running on Mac OS and Linux send enough information to identify the exact OS. For example, here's my User-Agent header:

Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.7) Gecko/2009030423 Ubuntu/8.10 (intrepid) Firefox/3.0.7

You can see that I'm running Ubuntu 8.10 Intrepid Ibex.

And here's what Firefox and Safari 4 Beta report on my MacBook Pro:

Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.7) Gecko/2009021906 Firefox/3.0.7

Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/528.16 (KHTML, like Gecko) Version/4.0 Safari/528.16

Windows browsers, on the other hand, usually only report the OS version and not the specific package (Pro, Business, etc.):

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:x.x.x) Gecko/20041107 Firefox/x.x

Determine Browser's Version

You can get it easily with get_browser()

<?php
$browser = get_browser();
$version = $browser->version;

PHP browser and os detection showing chrome instead of opera

Code logic : it finds Opera first in the loop, but then meets "Chrome", which also matches. So the last result found is kept. I suggest :

foreach ($browser_array as $regex => $value) { 

if (preg_match($regex, $user_agent) && $browser == "Unknown Browser")
$browser = $value;

}

This way, if the browser has already been found, it won't change it to the new value. Make sure to sort your array correctly though.

In your case, this code would find Opera first (matching OPR), and ignore Chrome afterwards (even though Opera matches with "Chrome"). The problem with that code is that this time, Chrome will be the problem (it will be detected as Opera). You should use more specific REGEX.

The fact that some browsers are partially signed as others is history-related. Some browsers take their code from others, and are therefore recognized differently.

Easiest Way OS Detection With PHP?

For an easy solution have a look here.
The user-agent header might reveal some OS information, but i wouldn't count on that.

For your use case i would do an ajax call using javascript from the client side to inform your server of the client's OS. And do it waterproof.

Here is an example.

Javascript (client side, browser detection + ajax call ):

window.addEvent('domready', function() { 
if (BrowserDetect) {
var q_data = 'ajax=true&browser=' + BrowserDetect.browser + '&version=' + BrowserDetect.version + '&os=' + BrowserDetect.OS;
var query = 'record_browser.php'
var req = new Request.JSON({url: query, onComplete: setSelectWithJSON, data: q_data}).post();
}
});

PHP (server side):

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
$session = session_id();
$user_id = isset($user_id) ? $user_id : 0;
$browser = isset($_POST['browser']) ? $_POST['browser'] : '';
$version = isset($_POST['version']) ? $_POST['version'] : '';
$os = isset($_POST['os']) ? $_POST['os'] : '';

// now do here whatever you like with this information
}

Is it possible to detect what operating system a user is coming from using PHP? (mac or windows)

Try the get_browser() function that's built into PHP.

$browser = get_browser(null, true);
echo "Platform: " . $browser["platform"] . "\n";

How to find the version of windows with php

Here is a nice bit of code to do what you want

<?php
$OSList = array
(
// Match user agent string with operating systems
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows 7' => '(Windows NT 6.1)',
'Windows 8' => '(Windows NT 6.2)',
'Windows 8.1' => '(Windows NT 6.3)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME'
);

// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match)
{
// Find a match
if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
{
// We found the correct match
break;
}
}
echo "We detect you are using ".$CurrOS."<br style='clear:both'>";
if ($CurrOS == "Windows XP")
{
echo "The alternative browsers you can download are:<br style='clear:both'><a target='_blank' href='http://google.com/chrome' style='color:white'>Google Chrome</a><br style='clear:both'><a target='_blank' href='http://mozilla.org/firefox' style='color:white'>Mozilla Firefox</a><br style='clear:both'><a target='_blank' href='http://opera.com' style='color:white'>Opera<br>";
}
elseif ($CurrOS == "Windows Vista")
{
echo "The alternative browsers you can download are:<br style='clear:both'><a target='_blank' href='http://google.com/chrome' style='color:white'>Google Chrome</a><br style='clear:both'><a target='_blank' href='http://mozilla.org/firefox' style='color:white'>Mozilla Firefox</a><br style='clear:both'><a target='_blank' href='http://opera.com' style='color:white'>Opera<br>";
}
elseif ($CurrOS == "Windows 7")
{
echo "The alternative browsers you can download are:<br style='clear:both'><a target='_blank' href='http://google.com/chrome' style='color:white'>Google Chrome</a><br style='clear:both'><a target='_blank' href='http://mozilla.org/firefox' style='color:white'>Mozilla Firefox</a><br style='clear:both'><a target='_blank' href='http://windows.microsoft.com/en-us/internet-explorer/ie-10-worldwide-languages' style='color:white'>Internet Explorer 10</a><br style='clear:both'><a target='_blank' href='http://windows.microsoft.com/en-us/internet-explorer/ie-11-worldwide-languages' style='color:white'>Internet Explorer 11</a><br style='clear:both'><a target='_blank' href='http://opera.com' style='color:white'>Opera<br>";
}
elseif ($CurrOS == "Windows 8")
{
echo "The alternative browsers you can download are:<br style='clear:both'><a target='_blank' href='http://google.com/chrome' style='color:white'>Google Chrome</a><br style='clear:both'><a target='_blank' href='http://mozilla.org/firefox' style='color:white'>Mozilla Firefox</a><br style='clear:both'><a target='_blank' href='http://windows.microsoft.com/en-us/internet-explorer/ie-10-worldwide-languages' style='color:white'>Internet Explorer 10</a><br style='clear:both'><a target='_blank' href='http://opera.com' style='color:white'>Opera<br>";
}
elseif ($CurrOS == "Windows 8.1")
{
echo "The alternative browsers you can download are:<br style='clear:both'><a target='_blank' href='http://google.com/chrome' style='color:white'>Google Chrome</a><br style='clear:both'><a target='_blank' href='http://mozilla.org/firefox' style='color:white'>Mozilla Firefox</a><br style='clear:both'><a target='_blank' href='http://windows.microsoft.com/en-us/internet-explorer/ie-11-worldwide-languages' style='color:white'>Internet Explorer 11</a><br style='clear:both'><a target='_blank' href='http://opera.com' style='color:white'>Opera<br>";
}
elseif ($CurrOs == "Windows ME" || $CurrOs == "Windows 98" || $CurrOs == "Windows 2000")
{
echo "The alternative browsers you can download are:<br style='clear:both'><a target='_blank' href='http://opera.com' style='color:white'>Opera<br>";
}
else
{
echo "<br>The version of windows you are currently using is not supported by any browsers better than Internet Explorer. We recommend you upgrade to a Windows XP, 7 or 8 machine to enjoy the best of the web<br>";
}

?>



Related Topics



Leave a reply



Submit