Skip to content

Instantly share code, notes, and snippets.

@Xennis
Created February 25, 2014 01:04
Show Gist options
  • Select an option

  • Save Xennis/9200608 to your computer and use it in GitHub Desktop.

Select an option

Save Xennis/9200608 to your computer and use it in GitHub Desktop.
<?php
/**
* Interpretiert den "User Agent" des Browser richtig.
*
* Supported Browser: Chrome, Firefox, IE, Opera, Safari
*
*/
class x11_detectBrowser
{
/**
* User agent string
*
* @var string
*/
private $_userAgent;
/**
* Browser id (in the order specified)
*
* @var array
*/
private $_supportedBrowsers = array ('opera', 'chrome', 'msie', 'safari', 'firefox');
/**
* Browser information (date: 05.09.2012)
*
* @var array
*/
private $_browserInfo = array(
'unkown' => array(
'name' => 'unkown',
'site' => 'unkown',
'version' => 'unkown'
),
'chrome' => array(
'name' => 'Google Chrome',
'site' => 'http://google.com/chrome',
'version' => '21'
),
'firefox' => array(
'name' => 'Mozilla Firefox',
'site' => 'http://firefox.com',
'version' => '15'
),
'msie' => array(
'name' => 'Windows Internet Explorer',
'site' => 'http://microsoft.com/windows/internet-explorer',
'version' => '10'
),
'opera' => array(
'name' => 'Opera',
'site' => 'http://opera.com',
'version' => '12'
),
'safari' => array(
'name' => 'Safari',
'site' => 'http://apple.com/safari',
'version' => '6'
)
);
/**
* Browser id (default: "unkown")
*
* @var string
*/
private $_id = 'unkown';
/**
* Browser version (default: "unkown")
*
* @var string
*/
private $_version = 'unkown';
/**
* Construct detectBrowser object.
*
* @param string|null Null or user agent string
*/
public function __construct( $userAgent = NULL )
{
if( is_empty( $userAgent ) ) {
$userAgent = $_SERVER['HTTP_USER_AGENT'];
}
$this->_userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
// Detect browser id
for($i = 0; $i < count($this->_supportedBrowsers); $i++) {
if(preg_match('/' . $this->_supportedBrowsers[$i] . '/i', $this->_userAgent)) {
$this->_id = $this->_supportedBrowsers[$i];
break;
}
}
if($this->_id !== 'unkown') {
// Detect browser version
$search = array ('version/', $this->_id . '/', $this->_id . ' ');
for($i = 0; i < count($search); $i++) {
$version = strstr($agent, $search[$i]);
$version = strstr($version, '.', true);
$version = str_replace($search[$i], '', $version);
if($version !== '') {
$this->_version = $version;
break;
}
}
}
}
/**
* Check browser version is old.
*
* @return boolean True, if browser version is old
*/
public function checkBrowserIsOld()
{
if($this->_version < $this->getLatestVersion()) {
return true;
} else {
return false;
}
}
/**
* Get user agent string.
*
* @return string User agent string
*/
public function getUserAgent()
{
return $this->_userAgent;
}
/**
* Get browser id (e.g. msie = Windows Internet Explorer).
*
* @return string Browser id
*/
public function getId()
{
return $this->_id;
}
/**
* Get browser version.
*
* @return string Browser version
*/
public function getVersion()
{
return $this->_version;
}
/**
* Get browser name.
*
* @return string Browser name
*/
public function getName()
{
return $this->_browserInfo[$this->_id]['name'];
}
/**
* Get browser website.
*
* @return string Browser website
*/
public function getWebsite()
{
return $this->_browserInfo[$this->_id]['website'];
}
/**
* Get latest browser version.
*
* @return string Latest browser version
*/
public function getLatestVersion()
{
return $this->_browserInfo[$this->_id]['version'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment