bonjour
perso je trouve ton code très sale et très mal structuré. il faut toujours penser à séparer le code du html.
voici une classe qui devrait t'aider à faire la même chose:
<?php /**
* IrivenCMS Pro : Rapid Development Framework (http://www.iriven.com)
* Copyright 2008-2013, Iriven France Software, Inc.
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2008-2013, Iriven France Software, Inc.
* @link http://www.iriven.com IrivenCMS Project
* @package IrivenCMS.Utility
* @since IrivenCMS Pro v 1.0.4
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class UserAgent
{
private $agent = null;
private $isBrowser = false;
private $isMobile = false;
private $isRobot = false;
private $platform = '';
private $browser = '';
private $version = '';
private $mobile = '';
private $robot = '';
private $charsets = array();
private $languages = array();
private $platforms = array (
'Windows NT 6.2' => 'Windows 8',
'Windows NT 6.1' => 'Windows 7',
'windows nt 6.0' => 'Windows Longhorn',
'windows nt 5.2' => 'Windows 2003',
'windows nt 5.0' => 'Windows 2000',
'windows nt 5.1' => 'Windows XP',
'windows nt 4.0' => 'Windows NT 4.0',
'winnt4.0' => 'Windows NT 4.0',
'winnt 4.0' => 'Windows NT',
'winnt' => 'Windows NT',
'windows 98' => 'Windows 98',
'win98' => 'Windows 98',
'windows 95' => 'Windows 95',
'win95' => 'Windows 95',
'windows' => 'Unknown Windows OS',
'os x' => 'Mac OS X',
'ppc mac' => 'Power PC Mac',
'freebsd' => 'FreeBSD',
'ppc' => 'Macintosh',
'linux' => 'Linux',
'debian' => 'Debian',
'sunos' => 'Sun Solaris',
'beos' => 'BeOS',
'apachebench' => 'ApacheBench',
'aix' => 'AIX',
'irix' => 'Irix',
'osf' => 'DEC OSF',
'hp-ux' => 'HP-UX',
'netbsd' => 'NetBSD',
'bsdi' => 'BSDi',
'openbsd' => 'OpenBSD',
'gnu' => 'GNU/Linux',
'unix' => 'Unknown Unix OS'
);
private $browsers = array(
'Flock' => 'Flock',
'Chrome' => 'Google Chrome',
'Opera' => 'Opera',
'MSIE' => 'Internet Explorer',
'Internet Explorer' => 'Internet Explorer',
'Shiira' => 'Shiira',
'Firefox' => 'Firefox',
'Chimera' => 'Chimera',
'Phoenix' => 'Phoenix',
'Firebird' => 'Firebird',
'Camino' => 'Camino',
'Netscape' => 'Netscape',
'OmniWeb' => 'OmniWeb',
'Safari' => 'Safari',
'Mozilla' => 'Mozilla',
'Konqueror' => 'Konqueror',
'icab' => 'iCab',
'Lynx' => 'Lynx',
'Links' => 'Links',
'hotjava' => 'HotJava',
'amaya' => 'Amaya',
'IBrowse' => 'IBrowse',
'NCSA_Mosaic' => 'Mosaic'
);
private $mobiles = array(
'mobileexplorer' => 'Mobile Explorer',
'palmsource' => 'Palm',
'palmscape' => 'Palmscape',
// Phones and Manufacturers
'motorola' => "Motorola",
'nokia' => "Nokia",
'palm' => "Palm",
'iphone' => "Apple iPhone",
'ipad' => "iPad",
'ipod' => "Apple iPod Touch",
'sony' => "Sony Ericsson",
'ericsson' => "Sony Ericsson",
'blackberry' => "BlackBerry",
'cocoon' => "O2 Cocoon",
'blazer' => "Treo",
'lg' => "LG",
'amoi' => "Amoi",
'xda' => "XDA",
'mda' => "MDA",
'privateio' => "Vario",
'htc' => "HTC",
'samsung' => "Samsung",
'sharp' => "Sharp",
'sie-' => "Siemens",
'alcatel' => "Alcatel",
'benq' => "BenQ",
'ipaq' => "HP iPaq",
'mot-' => "Motorola",
'playstation portable' => "PlayStation Portable",
'hiptop' => "Danger Hiptop",
'nec-' => "NEC",
'panasonic' => "Panasonic",
'philips' => "Philips",
'sagem' => "Sagem",
'sanyo' => "Sanyo",
'spv' => "SPV",
'zte' => "ZTE",
'sendo' => "Sendo",
// Operating Systems
'symbian' => "Symbian",
'SymbianOS' => "SymbianOS",
'elaine' => "Palm",
'palm' => "Palm",
'series60' => "Symbian S60",
'windows ce' => "Windows CE",
// Browsers
'obigo' => "Obigo",
'netfront' => "Netfront Browser",
'openwave' => "Openwave Browser",
'mobilexplorer' => "Mobile Explorer",
'operamini' => "Opera Mini",
'opera mini' => "Opera Mini",
// Other
'digital paths' => "Digital Paths",
'avantgo' => "AvantGo",
'xiino' => "Xiino",
'noprivatera' => "Noprivatera Transcoder",
'vodafone' => "Vodafone",
'docomo' => "NTT DoCoMo",
'o2' => "O2",
// Fallback
'mobile' => "Generic Mobile",
'wireless' => "Generic Mobile",
'j2me' => "Generic Mobile",
'midp' => "Generic Mobile",
'cldc' => "Generic Mobile",
'up.link' => "Generic Mobile",
'up.browser' => "Generic Mobile",
'smartphone' => "Generic Mobile",
'cellphone' => "Generic Mobile"
);
// There are hundreds of bots but these are the most common.
private $robots = array(
'googlebot' => 'Googlebot',
'msnbot' => 'MSNBot',
'slurp' => 'Inktomi Slurp',
'yahoo' => 'Yahoo',
'askjeeves' => 'AskJeeves',
'fastcrawler' => 'FastCrawler',
'infoseek' => 'InfoSeek Robot 1.0',
'lycos' => 'Lycos'
);
/*
*
*/
public function __construct()
{
$this->agent = isset($_SERVER'HTTP_USER_AGENT'])? trim($_SERVER'HTTP_USER_AGENT']):null;
is_null($this->agent) or $this->loadCompilator();
}
/**
* Set the Platform
*
* @access private
* @return mixed
*/
private function setPlatform()
{
foreach ($this->platforms as $key => $val)
{
if (preg_match('#'.preg_quote($key).'#i', $this->agent))
{
$this->platform = $val;
return TRUE;
}
}
$this->platform = 'Unknown Platform';
}
/**
* Set the Browser
*
* @access private
* @return bool
*/
private function setBrowser()
{
foreach ($this->browsers as $key => $val)
{
if (preg_match('#'.preg_quote($key).'.*?([0-9\.]+)#i', $this->agent, $match))
{
$this->isBrowser = TRUE;
$this->version = $match[1] ;
$this->browser = $val ;
$this->setMobile();
return TRUE;
}
}
return FALSE;
}
/**
* Set the Mobile Device
*
* @access private
* @return bool
*/
private function setMobile()
{
foreach ($this->mobiles as $key => $val)
{
if (FALSE !== (strpos(strtolower($this->agent), $key)))
{
$this->isMobile = TRUE;
$this->mobile = $val;
return TRUE;
}
}
return FALSE;
}
/**
* Set the Robot
*
* @access private
* @return bool
*/
private function setRobot()
{
foreach ($this->robots as $key => $val)
{
if (preg_match('#'.preg_quote($key).'#i', $this->agent))
{
$this->isRobot = TRUE;
$this->robot = $val;
return TRUE;
}
}
return FALSE;
}
/**
* Set the accepted character sets
*
* @access private
* @return void
*/
private function setCharsets()
{
if ((count($this->charsets) == 0) AND isset($_SERVER'HTTP_ACCEPT_CHARSET']) AND $_SERVER'HTTP_ACCEPT_CHARSET'] != '')
{
$charsets = preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER'HTTP_ACCEPT_CHARSET'])));
$this->charsets = explode(',', $charsets);
}
if (count($this->charsets) == 0)
{
$this->charsets = array('Undefined');
}
}
/**
* Is Browser
*
* @access public
* @return bool
*/
public function isBrowser()
{
if ( ! $this->isBrowser)
{
return FALSE;
}
return TRUE;
}
/**
* Is Robot
*
* @access public
* @return bool
*/
public function isRobot()
{
if ( ! $this->isRobot)
{
return FALSE;
}
return TRUE;
}
/**
* Is Mobile
*
* @access public
* @return bool
*/
public function isMobile($key = NULL)
{
if ( ! $this->isMobile)
{
return FALSE;
}
return TRUE;
}
/**
* Is this a referral from another site?
*
* @access public
* @return bool
*/
public function isReferral()
{
if ( ! isset($_SERVER'HTTP_REFERER']) OR $_SERVER'HTTP_REFERER'] == '')
{
return FALSE;
}
return TRUE;
}
/**
* Agent String
*
* @access public
* @return string
*/
public function agentString()
{
return $this->agent;
}
/**
* Get Platform
*
* @access public
* @return string
*/
public function getPlatform()
{
return $this->platform;
}
/**
* Get Browser Name
*
* @access public
* @return string
*/
public function browser()
{
return $this->browser;
}
/**
* Get the Browser Version
*
* @access public
* @return string
*/
public function version()
{
return $this->version;
}
/**
* Get The Robot Name
*
* @access public
* @return string
*/
public function robot()
{
return $this->robot;
}
/**
* Get the Mobile Device
*
* @access public
* @return string
*/
public function mobile()
{
return $this->mobile;
}
/**
* Get the referrer
*
* @access public
* @return bool
*/
public function referrer()
{
return ( ! isset($_SERVER'HTTP_REFERER']) OR $_SERVER'HTTP_REFERER'] == '') ? '' : trim($_SERVER'HTTP_REFERER']);
}
/**
* Get the accepted languages
*
* @access public
* @return array
*/
public function languages()
{
if (count($this->languages) == 0)
{
$this->setLanguages();
}
return $this->languages;
}
/**
* Get the accepted Character Sets
*
* @access public
* @return array
*/
public function charsets()
{
if (count($this->charsets) == 0)
{
$this->setCharsets();
}
return $this->charsets;
}
/**
* Test for a particular language
*
* @access public
* @return bool
*/
public function isAcceptedLang($lang = 'en')
{
return (in_array(strtolower($lang), $this->languages(), TRUE));
}
/**
* Test for a particular character set
*
* @access public
* @return bool
*/
public function isAcceptedCharset($charset = 'utf-8')
{
return (in_array(strtolower($charset), $this->charsets(), TRUE));
}
/**
* Compile the User Agent Data
*
* @access private
* @return bool
*/
private function loadCompilator()
{
$this->setPlatform();
foreach (array('setRobot', 'setBrowser', 'setMobile') as $function)
{
if ($this->$function() === TRUE)
{
break;
}
}
}
/*fin de la classe*/
}
par rapport à ton code, tu devrais pouvoir l'utiliser ainsi:
$browserRestriction = array($browser1=>$minVersion1,'......',$browserN=>$minVersionN);
$message = '';
include('UserAgent.php');
$ua = new UserAgent();
if($ua->isBrowser())
{
if(array_key_exists($ua->browser(),$browserRestriction))
{
if($browserRestriction$ua->browser()]>=$ua->version())
$message .= 'ce site est incompatible avec une version du navigateur '.$ua->browser().' inférieure à '.$browserRestriction$ua->browser()].'. Votre version: '.$ua->version();
}
}
empty($message) or die($message);
cordialement