首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将函数的访问限制为每24小时一次,PHP + MySQL

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

Stack Overflow用户
提问于 2016-12-22 17:31:15
回答 1查看 748关注 0票数 0

根据用户IP地址,我希望将我创建的函数的访问限制为每24小时一次。如果MySQL记录超过24小时,我也希望PHP脚本删除它。

​

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

如果用户已经使用了该函数,但自使用该函数以来已过了24小时,则删除MySQL记录并让脚本继续运行。

我迷路了,正如你所看到的,我也丢失了一些删除旧记录的声明(-24小时)。

有人能给我举个例子来说明如何做到这一点吗?谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-22 18:33:06

  1. 获取客户端的IP地址,并在记录不存在的情况下用当前日期和时间存储它。
  2. 获取记录并在其日期和时间值中添加24小时,并在每次执行脚本时使用当前日期和时间检查它。
  3. 您需要if else条件语句来检查24小时时间是否结束。在此基础上,您将控制要执行的函数。

我想我不想写太多的理论。在这里,我编写了代码的样式:

代码语言:javascript
复制
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
    }
}

我不能给你写完整的代码。我只能给你一些提示。你需要用它来构建代码。希望我给了你正确的提示能帮到你。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41288794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档