reliable user browser detection with php
User Agent can be faked and its better not to depend upon user agent string rather then you can use a CSS3 media queries if you only want to detect orientation (you can explore the CSS of the famous responsive framework bootstrap to check how you can handle the orientation part using CSS)
Here is little CSS :
@media only screen and (max-width: 999px) {
/* rules that only apply for canvases narrower than 1000px */
}
@media only screen and (device-width: 768px) and (orientation: landscape) {
/* rules for iPad in landscape orientation */
}
@media only screen and (min-device-width: 320px) and (max-device-width: 480px) {
/* iPhone, Android rules here */
}
Read more : About CSS orientation detection
OR You can find the orientation using JavaScript :
// Listen for orientation changes
window.addEventListener("orientationchange", function() {
// Announce the new orientation number
alert(window.orientation);
}, false);
Read more : About detect orientation using JS
Finally if you really wants to go with user agent string then this code will help you alot, work fine almost on every browser :
<?php
class BrowserDetection {
private $_user_agent;
private $_name;
private $_version;
private $_platform;
private $_basic_browser = array (
'Trident\/7.0' => 'Internet Explorer 11',
'Beamrise' => 'Beamrise',
'Opera' => 'Opera',
'OPR' => 'Opera',
'Shiira' => 'Shiira',
'Chimera' => 'Chimera',
'Phoenix' => 'Phoenix',
'Firebird' => 'Firebird',
'Camino' => 'Camino',
'Netscape' => 'Netscape',
'OmniWeb' => 'OmniWeb',
'Konqueror' => 'Konqueror',
'icab' => 'iCab',
'Lynx' => 'Lynx',
'Links' => 'Links',
'hotjava' => 'HotJava',
'amaya' => 'Amaya',
'IBrowse' => 'IBrowse',
'iTunes' => 'iTunes',
'Silk' => 'Silk',
'Dillo' => 'Dillo',
'Maxthon' => 'Maxthon',
'Arora' => 'Arora',
'Galeon' => 'Galeon',
'Iceape' => 'Iceape',
'Iceweasel' => 'Iceweasel',
'Midori' => 'Midori',
'QupZilla' => 'QupZilla',
'Namoroka' => 'Namoroka',
'NetSurf' => 'NetSurf',
'BOLT' => 'BOLT',
'EudoraWeb' => 'EudoraWeb',
'shadowfox' => 'ShadowFox',
'Swiftfox' => 'Swiftfox',
'Uzbl' => 'Uzbl',
'UCBrowser' => 'UCBrowser',
'Kindle' => 'Kindle',
'wOSBrowser' => 'wOSBrowser',
'Epiphany' => 'Epiphany',
'SeaMonkey' => 'SeaMonkey',
'Avant Browser' => 'Avant Browser',
'Firefox' => 'Firefox',
'Chrome' => 'Google Chrome',
'MSIE' => 'Internet Explorer',
'Internet Explorer' => 'Internet Explorer',
'Safari' => 'Safari',
'Mozilla' => 'Mozilla'
);
private $_basic_platform = array(
'windows' => 'Windows',
'iPad' => 'iPad',
'iPod' => 'iPod',
'iPhone' => 'iPhone',
'mac' => 'Apple',
'android' => 'Android',
'linux' => 'Linux',
'Nokia' => 'Nokia',
'BlackBerry' => 'BlackBerry',
'FreeBSD' => 'FreeBSD',
'OpenBSD' => 'OpenBSD',
'NetBSD' => 'NetBSD',
'UNIX' => 'UNIX',
'DragonFly' => 'DragonFlyBSD',
'OpenSolaris' => 'OpenSolaris',
'SunOS' => 'SunOS',
'OS\/2' => 'OS/2',
'BeOS' => 'BeOS',
'win' => 'Windows',
'Dillo' => 'Linux',
'PalmOS' => 'PalmOS',
'RebelMouse' => 'RebelMouse'
);
function __construct($ua = '') {
if(empty($ua)) {
$this->_user_agent = (!empty($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:getenv('HTTP_USER_AGENT'));
}
else {
$this->_user_agent = $ua;
}
}
function detect() {
$this->detectBrowser();
$this->detectPlatform();
return $this;
}
function detectBrowser() {
foreach($this->_basic_browser as $pattern => $name) {
if( preg_match("/".$pattern."/i",$this->_user_agent, $match)) {
$this->_name = $name;
// finally get the correct version number
$known = array('Version', $pattern, 'other');
$pattern_version = '#(?<browser>' . join('|', $known).')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern_version, $this->_user_agent, $matches)) {
// we have no matching number just continue
}
// see how many we have
$i = count($matches['browser']);
if ($i != 1) {
//we will have two since we are not using 'other' argument yet
//see if version is before or after the name
if (strripos($this->_user_agent,"Version") < strripos($this->_user_agent,$pattern)){
@$this->_version = $matches['version'][0];
}
else {
@$this->_version = $matches['version'][1];
}
}
else {
$this->_version = $matches['version'][0];
}
break;
}
}
}
function detectPlatform() {
foreach($this->_basic_platform as $key => $platform) {
if (stripos($this->_user_agent, $key) !== false) {
$this->_platform = $platform;
break;
}
}
}
function getBrowser() {
if(!empty($this->_name)) {
return $this->_name;
}
}
function getVersion() {
return $this->_version;
}
function getPlatform() {
if(!empty($this->_platform)) {
return $this->_platform;
}
}
function getUserAgent() {
return $this->_user_agent;
}
function getInfo() {
return "<strong>Browser Name:</strong> {$this->getBrowser()}<br/>\n" .
"<strong>Browser Version:</strong> {$this->getVersion()}<br/>\n" .
"<strong>Browser User Agent String:</strong> {$this->getUserAgent()}<br/>\n" .
"<strong>Platform:</strong> {$this->getPlatform()}<br/>";
}
}
$obj = new BrowserDetection();
echo $obj->detect()->getInfo();
The above code works for me almost on every browser and i hope it will help you a bit.
Check this code , I've found this is useful. Don't check Mozilla because most browser use this as user agent string
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
echo 'Internet explorer';
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Trident') !== FALSE) //For Supporting IE 11
echo 'Internet explorer';
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== FALSE)
echo 'Mozilla Firefox';
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== FALSE)
echo 'Google Chrome';
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== FALSE)
echo "Opera Mini";
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== FALSE)
echo "Opera";
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== FALSE)
echo "Safari";
else
echo 'Something else';
Using an existing method (ie get_browser
) is probably better than writing something yourself, since it has (better) support and will be updated with newer versions. There might be also usable libraries out there for getting the browser id in a reliable way.
Decoding the $_SERVER['HTTP_USER_AGENT']
is difficult, since a lot of browsers have quite similar data and tend to (mis)use it for their own benefits. But if you really want to decode them, you could use the information on this page for all available agent ids.
This page also shows that your example output indeed belongs to IE 7. More information about the fields in the agent id itself can be found on this page, but as I said already browsers tend to use it for their own benefits and it could be in a (slightly) other format.