将函数的访问限制为每24小时PHP + MySQL一次

时间:2016-12-22 17:31:15

标签: php mysql

我想根据用户的IP地址限制我每24小时创建一次的功能的访问权限。我还希望PHP脚本能够删除MySQL记录,如果它超过24小时。

如果用户已在24小时内使用过该功能,请向他们显示一条消息并阻止脚本继续运行。

如果用户已使用该功能但自使用该功能后已经过了24小时,请删除MySQL记录并让脚本继续运行。

我输了,你可以看到我也遗漏了一些删除旧记录的陈述(-24小时)..

有人能为我提供一个如何做到这一点的例子吗?感谢

1 个答案:

答案 0 :(得分:1)

  1. 获取客户端的IP地址,如果记录不存在,则将其与当前日期和时间一起存储。
  2. 获取记录并将24小时添加到其日期和时间值,并在每次执行脚本时使用当前日期和时间进行检查。
  3. 您需要if else条件语句来检查24小时时间是否结束。基于此,您将控制您想要的功能的执行。
  4. 我想我不想写很多理论。在这里,我已经编写了代码的样式:

    if(!$record_in_db) {
        // create record with the client's ip address and the current date and time
        // invoke the function you want - This is the code to trigger the function first time for the new IP address
    } else {
        // fetch_record_from_db
        // add 24 hours to its date and time value
        // check it with current date and time
    
        $record_date_time = new DateTime('22-12-2016 22:45:20'); // this value should be fetched from database
        $record_date_time_by_24_hours = $record_date_time->modify('+1 day');
        $current_date_time = new DateTime(date('d-m-Y H:i:s', strtotime('now')));
        $date_time_diff = $current_date_time->diff($record_date_time_by_24_hours);
    
        if($date_time_diff->invert == 0) {
            // Do something
        } else {
           // update the date and time of the record to NOW which is current date and time
           // invoke the function you want
        }
    }
    

    我无法给你写完整个代码。我只能给你一些提示。您需要从中构建代码。希望我已经给你正确的提示,可以帮助你。

相关问题