如何检测用户是否没有使用任何使用 JavaScript 或 PHP 的 Chrome、Firefox 或 Internet Explorer 浏览器?
如何使用 PHP 或 JavaScript 检测浏览器?
IT技术
php
javascript
2021-03-18 09:41:51
6个回答
我发现在 JS 中执行此操作的最佳方法是使用Quirksmode。我为 PHP 制作了一个,它应该可以与普通浏览器一起使用:
$browser = array(
'version' => '0.0.0',
'majorver' => 0,
'minorver' => 0,
'build' => 0,
'name' => 'unknown',
'useragent' => ''
);
$browsers = array(
'firefox', 'msie', 'opera', 'chrome', 'safari', 'mozilla', 'seamonkey', 'konqueror', 'netscape',
'gecko', 'navigator', 'mosaic', 'lynx', 'amaya', 'omniweb', 'avant', 'camino', 'flock', 'aol'
);
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$browser['useragent'] = $_SERVER['HTTP_USER_AGENT'];
$user_agent = strtolower($browser['useragent']);
foreach($browsers as $_browser) {
if (preg_match("/($_browser)[\/ ]?([0-9.]*)/", $user_agent, $match)) {
$browser['name'] = $match[1];
$browser['version'] = $match[2];
@list($browser['majorver'], $browser['minorver'], $browser['build']) = explode('.', $browser['version']);
break;
}
}
}
这是 JavaScript 代码,您可以通过它轻松检测浏览器。
var userAgent = navigator.userAgent.toLowerCase();
// Figure out what browser is being used.
var Browser = {
Version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
Chrome: /chrome/.test(userAgent),
Safari: /webkit/.test(userAgent),
Opera: /opera/.test(userAgent),
IE: /msie/.test(userAgent) && !/opera/.test(userAgent),
Mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent),
Check: function() { alert(userAgent); }
};
if (Browser.Chrome || Browser.Mozilla) {
// Do your stuff for Firefox and Chrome.
}
else if (Browser.IE) {
// Do something related to Internet Explorer.
}
else {
// The browser is Safari, Opera or some other.
}
实际上,PHP 中有一个函数get_browser。
get_browser() 中的PHP 代码完全适合我;)
<?php
function getBrowser()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version= "";
//First get the platform?
if (preg_match('/linux/i', $u_agent)) {
$platform = 'linux';
}
elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
$platform = 'mac';
}
elseif (preg_match('/windows|win32/i', $u_agent)) {
$platform = 'windows';
}
// Next get the name of the useragent yes separately and for good reason.
if (preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
{
$bname = 'Internet Explorer';
$ub = "MSIE";
}
elseif (preg_match('/Firefox/i',$u_agent))
{
$bname = 'Mozilla Firefox';
$ub = "Firefox";
}
elseif (preg_match('/Chrome/i',$u_agent))
{
$bname = 'Google Chrome';
$ub = "Chrome";
}
elseif (preg_match('/Safari/i',$u_agent))
{
$bname = 'Apple Safari';
$ub = "Safari";
}
elseif (preg_match('/Opera/i',$u_agent))
{
$bname = 'Opera';
$ub = "Opera";
}
elseif (preg_match('/Netscape/i',$u_agent))
{
$bname = 'Netscape';
$ub = "Netscape";
}
// Finally get the correct version number.
$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_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($u_agent,"Version") < strripos($u_agent,$ub)){
$version= $matches['version'][0];
}
else {
$version= $matches['version'][1];
}
}
else {
$version= $matches['version'][0];
}
// Check if we have a number.
if ($version==null || $version=="") {$version="?";}
return array(
'userAgent' => $u_agent,
'name' => $bname,
'version' => $version,
'platform' => $platform,
'pattern' => $pattern
);
}
// Now try it.
$ua=getBrowser();
$yourbrowser= "Your browser: " . $ua['name'] . " " . $ua['version'] . " on " .
$ua['platform'] . " reports: <br >" . $ua['userAgent'];
print_r($yourbrowser);
?>
1) 99.9% 准确检测器:BrowserDetection.php (示例)
2)最简单的功能(但不准确的欺骗):
<?php
function get_user_browser()
{
$u_agent = $_SERVER['HTTP_USER_AGENT']; $ub = '';
if(preg_match('/MSIE/i',$u_agent)) { $ub = "ie"; }
elseif(preg_match('/Firefox/i',$u_agent)) { $ub = "firefox"; }
elseif(preg_match('/Safari/i',$u_agent)) { $ub = "safari"; }
elseif(preg_match('/Chrome/i',$u_agent)) { $ub = "chrome"; }
elseif(preg_match('/Flock/i',$u_agent)) { $ub = "flock"; }
elseif(preg_match('/Opera/i',$u_agent)) { $ub = "opera"; }
return $ub;
}
?>
其它你可能感兴趣的问题