下面的脚本为所有漫游器访问创建一个日志文件,向我发送电子邮件,并在ip2location验证IP。在带有eregi函数的PHP5.2上它可以很好地工作,因此我在向每个bot变量添加正斜杠之后将eregi行修改为preg_match,并在wamp测试服务器上工作了几分钟,因为我得到了“ reg_match():定界符不能为字母数字或反斜杠”警告,但现在它将不起作用,也不会在visits.log文件中记录任何漫游器。
该脚本仍在下面给了我这三个警告,但是由于它们是警告并且已经开始起作用,因此我对它们并没有太多注意:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$to = "[email protected]";
$log = "./visits.log";
$dateTime = date("r");
$agents[] = "/googlebot/";
$spiders[] = "/Google/";
$spiders[] = "/Googlebot/";
$agents[] = "/slurp/";
$spiders[] = "/Slurp (Inktomi's robot, HotBot)/";
$agents[] = "/msnbot/";
$spiders[] = "/MSN Robot (MSN Search, search\.msn\.com)/";
$agents[] = "/yahoo\! slurp/";
$spiders[] = "/Yahoo! Slurp/";
$agents[] = "/bingbot/";
$spiders[] = "/Bing\.com/";
$ip= $_SERVER['REMOTE_ADDR'];
$found = false;
for ($spi = 0; $spi < count($spiders); $spi++)
if ($found = preg_match($agents[$spi], $_SERVER['HTTP_USER_AGENT']))
break;
if ($found) {
$url = "http://" . $_SERVER['SERVER_NAME']. $_SERVER['PHP_SELF'];
if ($_SERVER['QUERY_STRING'] != "") {
$url .= '?' . $_SERVER['QUERY_STRING'];
}
$line = $dateTime . " " . $spiders[$spi] . " " . $ip." @ " . $url;
$ip2location = "https://www.ip2location.com/".$_SERVER['REMOTE_ADDR'];
if ($log != "") {
if (@file_exists($log)) {
$mode = "a";
} else {
$mode = "w";
}
if ($f = @fopen($log, $mode)) {
@fwrite($f, $line . "\n");
@fclose($f);
}
}
if ($to != "") {
$to = "[email protected]";
$subject = $spiders[$spi]. " crawled your site";
$body = "$line". "\xA\xA" ."Whois verification available at: $ip2location";
mail($to, $subject, $body);
}
}
if ($_REQUEST["js"]) {
header("Content-Type: image/gif\r\n");
header("Cache-Control: no-cache, must-revalidate\r\n");
header("Pragma: no-cache\r\n");
@readfile("visits.gif");
}
?>
括号在php 7 preg_match的正则表达式中有特殊含义。只是逃脱它们应该工作正常。至于第一个警告而不是仅使用coint($agents)
,请使用count($agents) - 1
正弦数组索引从零开始或仅使用foreach
。第二次警告使用if(isset($_REQUEST ["js"])
祝你好运
a)$ spiders中有6个元素,而$ agents中只有5个元素,这将导致有关偏移5和空正则表达式的警告。 Googlebot翻倍:
$spiders[] = "/Google/";
$spiders[] = "/Googlebot/";
删除一个条目
b)if ($_REQUEST["js"]) {
应该替换为:
[if (isset($_REQUEST["js"])) {
并根据期望的isset之后的值,应检查该值-例如,如果您根据true
进行验证:
if (isset($_REQUEST["js"]) && $_REQUEST['js'] === true) {