我在我的标题中有这个代码,如果他们有特定的旧浏览器,它应该将用户重定向到另一个页面。 我不希望机器人访问该网站时执行此JavaScript代码。我为此创建了一个PHP条件,但是我的httpd访问日志仍然显示机器人正在运行此代码。 我知道,因为我使用JSNlog来记录javascript错误,并且在执行我的代码时会触发错误。
这是我的代码:
<?php
if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'])) {
//do nothing
}else{
?>
<script type="text/javascript">
function getBrowserData(nav) {
var data = {};
var ua = data.uaString = nav.userAgent;
var browserMatch = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*([\d\.]+)/i) || [];
if (browserMatch[1]) { browserMatch[1] = browserMatch[1].toLowerCase(); }
var operaMatch = browserMatch[1] === 'chrome';
if (operaMatch) { operaMatch = ua.match(/\bOPR\/([\d\.]+)/); }
if (/trident/i.test(browserMatch[1])) {
var msieMatch = /\brv[ :]+([\d\.]+)/g.exec(ua) || [];
data.name = 'msie';
data.version = msieMatch[1];
}
else if (operaMatch) {
data.name = 'opera';
data.version = operaMatch[1];
}
else if (browserMatch[1] === 'safari') {
var safariVersionMatch = ua.match(/version\/([\d\.]+)/i);
if (safariVersionMatch !== null) {
data.name = 'safari';
data.version = safariVersionMatch[1];
}
}
else {
data.name = browserMatch[1];
data.version = browserMatch[2];
}
var versionParts = [];
if (data.version) {
var versionPartsMatch = data.version.match(/(\d+)/g) || [];
for (var i=0; i < versionPartsMatch.length; i++) {
versionParts.push(versionPartsMatch[i]);
}
if (versionParts.length > 0) { data.majorVersion = versionParts[0]; }
}
data.name = data.name || '(unknown browser name)';
data.version = {
full: data.version || '(unknown full browser version)',
parts: versionParts,
major: versionParts.length > 0 ? versionParts[0] : '(unknown major browser version)'
};
return data;
};
var brData = getBrowserData(window.navigator || navigator);
if (
(brData.name == 'opera' && brData.version.major < 10) ||
(brData.name == 'msie' && brData.version.major < 10) ||
(brData.name == 'firefox' && brData.version.major < 27) ||
(brData.name == 'safari' && brData.version.major < 6)
) {
window.location = "https://www.example.com/outdated.htm";
}
// }
</script>
<?php } ?>
这是我在Apache日志中看到的错误:
"POST /jsnlog.logger.php HTTP/1.1" 200 40 "https://www.example.com/some-url" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
哪个匹配我的JSNlog错误日志:
[JSPHPLog]: {"msg":"Exception!","errorMsg":"Uncaught TypeError: Cannot read property '1' of null","url":""https://www.example.com/some-url","line number":186}
第186行是上述javascript代码中的一行。
我的病情有问题吗?如何让机器人不运行/看到这段代码?