此代码不适用于 IE8 和其他旧版本的 IE。
我重写了代码,现在我有两种方法,但仍然失败......
为什么?什么找不到?
我调用函数的方式:
if (BrowserVersionChecker::is_old_browser()==1) {
//popup here
}
班上:
<?php
class BrowserVersionChecker {
/**
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 is_old_browser() {
$old = 0;
$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';
}
$version = intval($version);
if (($name == 'opera') && ($version <= 11)) {
$old = 1;
}
if (($name == 'safari') && ($version <= 5)) {
$old = 1;
}
if (($name == 'msie') && ($version <= 8)) {
$old = 1;
}
if (($name == 'mozilla') && ($version <= 7)) {
$old = 1;
}
// $arr = array(
// 'name' => $name,
// 'version' => $version,
// 'platform' => $platform,
// 'userAgent' => $userAgent
// );
//
// echo '<pre>';
// print_r($arr);
return $old;
// return array(
// 'name' => $name,
// 'version' => $version,
// 'platform' => $platform,
// 'userAgent' => $userAgent
// );
}
//not used any more - deprecated
public static function is_old_browser2() {
$old = 0;
$browser = get_browser(null, true);
print_r($browser);
if (!empty($_SERVER['HTTP_USER_AGENT'])) {
// IE <= 7
// User Agent: Opera/9.80 (Windows NT 6.1; U; en) Presto/2.10.229 Version/11.61
if (preg_match('#msie\s+(\d+)\.(\d+)#si', $_SERVER['HTTP_USER_AGENT'], $matches)) {
if ($matches[1] <= 8) {
$old = 1;
}
}
// Firefox <= 7
// User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
elseif (preg_match('#Firefox/(\d+)\.(\d+)[\.\d]+#si', $_SERVER['HTTP_USER_AGENT'], $matches)) {
if ($matches[1] <= 7) {
$old = 1;
}
}
// Safari < 5
// User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.52.7 (KHTML, like Gecko) Version/5.1.2 Safari/534.52.7
elseif (preg_match('#Version/(\d+)[\.\d]+ Safari/[\.\d]+#si', $_SERVER['HTTP_USER_AGENT'], $matches)) {
if ($matches[1] < 5) {
$old = 1;
}
}
// opera < 11
// User Agent: Opera/9.80 (Windows NT 6.1; U; en) Presto/2.10.229 Version/11.61
elseif (preg_match('#Opera/[\.\d]+.*?Version/(\d+)[\.\d]+#si', $_SERVER['HTTP_USER_AGENT'], $matches)) {
if ($matches[1] < 11) {
$old = 1;
}
}
}
return $old;
}
}