根据用户IP地址,我希望将我创建的函数的访问限制为每24小时一次。如果MySQL记录超过24小时,我也希望PHP脚本删除它。
如果用户在24小时内已经使用了该函数,请向他们显示一条消息,并阻止脚本继续运行。
如果用户已经使用了该函数,但自使用该函数以来已过了24小时,则删除MySQL记录并让脚本继续运行。
我迷路了,正如你所看到的,我也丢失了一些删除旧记录的声明(-24小时)。
有人能给我举个例子来说明如何做到这一点吗?谢谢
发布于 2016-12-22 18:33:06
if else条件语句来检查24小时时间是否结束。在此基础上,您将控制要执行的函数。我想我不想写太多的理论。在这里,我编写了代码的样式:
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
}
}我不能给你写完整的代码。我只能给你一些提示。你需要用它来构建代码。希望我给了你正确的提示能帮到你。
https://stackoverflow.com/questions/41288794
复制相似问题