douche1936 2018-01-31 18:36
浏览 33
已采纳

基于时间的IP块与MySQL PHP

I am using ajax to store votes and currently trying to give every IP Adresse that has already voted a Block for 18 hours.

First I'm storing the IP and Time in the database but I can't seem to figure out how I check if the IP is found in my array and then if the time associated to the Ip is the matching with 18hours plus.

I am using a multidimensional array for each row in my database. This is what I have so far, but I am always getting either a 500 or unexpected end of JSON error.

 $currentip = $_SERVER['REMOTE_ADDR'];
    $time  = date('Y-m-d H:i:s');
    $exptime = date('Y-m-d H:i:s', strtotime('+18 Hours'));

    // Check IP
    $sqlip = "SELECT ip,exptime FROM `user_ip` ;";
    $storedip = array();
     if ($result = $this->databaseConnection->query($sqlip)){
         while($row = $result->fetch_assoc()) {
             $storedip = array($row['ip'], $row['exptime']);
            }       

      if(in_array($currentip, array_column($storedip, '0'))){
      $key = array_search($currentip, array_column($storedip, 0));

        if($time <= $storedip[$key][1]){
            echo json_encode("IP Block active", JSON_PRETTY_PRINT);
         }
      } 
      else{

         $setip = "INSERT INTO `user_ip` VALUES ('$currentip','$exptime');";
          if($this->executeDatabaseQuery($setip)){
        // Count Vote and do my stuff
          }
         }
     }
  • 写回答

1条回答 默认 最新

  • dongmiao520892 2018-01-31 18:39
    关注

    Your problem is here:

    $storedip = array($row['ip'], $row['exptime']);
    

    It should be:

    $storedip[] = array($row['ip'], $row['exptime']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用