猿问

我修改了脚本以使用php 7.1,将ergi更改为preg_match,脚本在wamp上工作了几分钟

下面的脚本为所有机器人访问创建一个日志文件,向我发送电子邮件,并在 ip2location 验证 IP。它与带有eri函数的PHP5.2一起工作得很好,所以我修改了ergi行preg_match,并在向每个机器人变量添加正斜杠后在我的wamp测试服务器上工作了几分钟,因为我得到一个“reg_match():分隔符不能是字母数字或反斜杠”警告,但现在它不起作用,也不会在访问.log文件中记录任何机器人。

脚本仍然在下面给了我这三个警告,但是由于它们是警告并且已经开始工作,所以我没有太多关注它们:

  • 注意:未定义的偏移量:C:\wamp\www\visits 中的 5 .php在第 28 行

  • 警告:preg_match():在第 28 行的 C:\wamp\www\visits.php中为空正则表达式

  • 注意:未定义的索引:c:\wamp\www\visits中的js.php第62行

<?php


error_reporting(E_ALL);

ini_set('display_errors', 1);


  $to = "email@here.com";


  $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@here.com";

$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");

  }


?>


函数式编程
浏览 102回答 2
2回答

胡说叔叔

a) $spiders只有 6 个元素,$agents只有 5 个元素,这会导致有关偏移量 5 和空正则表达式的警告。Googlebot翻了一番:&nbsp;&nbsp;$spiders[]&nbsp;=&nbsp;"/Google/"; &nbsp;&nbsp;&nbsp;&nbsp;$spiders[]&nbsp;=&nbsp;"/Googlebot/";删除一个条目b) 应改为:if ($_REQUEST["js"]) {if (isset($_REQUEST["js"])) {并且根据您期望在之后设置的值,应检查该值 - 例如,如果您根据以下条件进行验证:trueif (isset($_REQUEST["js"]) && $_REQUEST['js'] === true) {

浮云间

括号在 php 7 preg_match 的正则表达式中具有特殊含义。只是逃离他们,它应该工作正常。至于第一个警告,而不是只使用正弦数组索引从零开始或只使用。第二次警告使用 好运coint($agents)count($agents) - 1foreachif(isset($_REQUEST ["js"])
随时随地看视频慕课网APP
我要回答