Gal*_*Gal 32 php browser-detection
试图用PHP检测用户的浏览器,$ _SERVER ['HTTP_USER_AGENT']是一种可靠的方法吗?我应该选择get_browser函数吗?您找到哪一个会带来更精确的结果?
如果这种方法是实用的,那么建议使用它来输出相关的CSS链接,例如:
if(stripos($_SERVER['HTTP_USER_AGENT'],"mozilla")!==false)
echo '<link type="text/css" href="mozilla.css" />';
Run Code Online (Sandbox Code Playgroud)
我注意到了这个问题,但我想澄清这是否适合面向CSS的检测.
更新:
真的很可疑:我试过echo $_SERVER['HTTP_USER_AGENT'];IE 7,这就是输出:
Mozilla/4.0(兼容; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30618)
Safari也给了"mozilla"一些奇怪的东西.是什么赋予了?
Ekr*_*que 59
检查这段代码,我发现这很有用.不要检查Mozilla,因为大多数浏览器都将此用作用户代理字符串
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';
Run Code Online (Sandbox Code Playgroud)
Veg*_*ger 16
使用现有方法(即get_browser)可能比自己编写一些东西更好,因为它具有(更好)支持并且将使用更新版本进行更新.可能还有可用的库可以以可靠的方式获取浏览器ID.
解码$_SERVER['HTTP_USER_AGENT']很困难,因为很多浏览器都有非常相似的数据,并且倾向于(误)使用它来获得自己的好处.但是,如果您真的想要对它们进行解码,则可以使用此页面上的信息来获取所有可用的代理ID.此页面还显示您的示例输出确实属于IE 7.有关代理ID本身中字段的更多信息可以在此页面上找到,但正如我所说,浏览器倾向于使用它来获取它们自己的好处,它可能在一个(稍微)其他格式.
小智 6
用户代理可以伪造,最好不要依赖于用户代理字符串,如果您只想检测方向,则可以使用 CSS3 媒体查询(您可以探索著名的响应式框架引导程序的 CSS 以检查如何处理方向部分使用 CSS)
这是小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 */
}
Run Code Online (Sandbox Code Playgroud)
阅读更多:关于 CSS 方向检测
或者您可以使用 JavaScript 找到方向:
// Listen for orientation changes
window.addEventListener("orientationchange", function() {
// Announce the new orientation number
alert(window.orientation);
}, false);
Run Code Online (Sandbox Code Playgroud)
阅读更多:关于使用 JS 检测方向
最后,如果你真的想使用用户代理字符串,那么这段代码会对你有很大帮助,几乎在每个浏览器上都能正常工作:
<?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();
Run Code Online (Sandbox Code Playgroud)
上面的代码几乎适用于所有浏览器,我希望它对你有所帮助。
class Browser {
/**
Figure out what browser is used, its version and the platform it is
running on.
The following code was ported in part from JQuery v1.3.1
*/
public static function detect() {
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
// Identify the browser. Check Opera and Safari first in case of spoof. Let Google Chrome be identified as Safari.
if (preg_match('/opera/', $userAgent)) {
$name = 'opera';
}
elseif (preg_match('/webkit/', $userAgent)) {
$name = 'safari';
}
elseif (preg_match('/msie/', $userAgent)) {
$name = 'msie';
}
elseif (preg_match('/mozilla/', $userAgent) && !preg_match('/compatible/', $userAgent)) {
$name = 'mozilla';
}
else {
$name = 'unrecognized';
}
// What version?
if (preg_match('/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/', $userAgent, $matches)) {
$version = $matches[1];
}
else {
$version = 'unknown';
}
// Running on what platform?
if (preg_match('/linux/', $userAgent)) {
$platform = 'linux';
}
elseif (preg_match('/macintosh|mac os x/', $userAgent)) {
$platform = 'mac';
}
elseif (preg_match('/windows|win32/', $userAgent)) {
$platform = 'windows';
}
else {
$platform = 'unrecognized';
}
return array(
'name' => $name,
'version' => $version,
'platform' => $platform,
'userAgent' => $userAgent
);
}
}
$browser = Browser::detect();
Run Code Online (Sandbox Code Playgroud)