php限制24小时,将函数的访问限制为每24小时PHP + MySQL一次(Limit the access of a function to once every 24 hours PHP + My...

将函数的访问限制为每24小时PHP + MySQL一次(Limit the access of a function to once every 24 hours PHP + MySQL)

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

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

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

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

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

I would like to limit the access of a function i've created to once every 24 hour based on the users IP address. I would also like the PHP script to delete the MySQL record if it's older than 24 hours.

If the user already has used the function within 24 hours, show them a message and prevent the script from continue running.

If the user already has used the function but 24 hours has passed since he used the function, delete the MySQL record and let the script continue running.

I'm lost and as you can see i am also missing some statements for deleting old records (-24 hours)..

Could anyone provide me with an example of how to do this? Thanks

原文:https://stackoverflow.com/questions/41288794

更新时间:2020-02-14 07:05

最满意答案

获取客户端的IP地址,并在记录不存在时将其与当前日期和时间一起存储。

获取记录并将24小时添加到其日期和时间值,并在每次执行脚本时使用当前日期和时间进行检查。

您需要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

}

}

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

Get client's IP address and store it with current date and time if the record doesn't exist.

Fetch the record and add 24 hours to its date and time value and check it with the current date and time every time the script is executed.

You need if else conditional statements to check if the 24 hours time is over or not. Based on that, you will control the execution of the function you want to.

I think I don't want to write much of theory. Here, I've written the pattern what the code looks like:

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

}

}

I can't write you the whole code. I could only give you some hints. You need to build the code from it. Hope I've given you right hints that could help you.

2016-12-22

相关问答

这是数据库的一个很好的候选者。 您可以拥有一个包含3个字段的表: IPAddress , Attemps , LastDate这样您就可以知道每个IP地址对当前的最后日期进行了多少尝试。 每当实际日期不再相同时,尝试计数重置为1.这是机制可能看起来像的伪代码: if (same date) {

if (attemps < 5) {

if (!try_connect()) {

attemps++;

}

} else {

...

假设“日期”是你的日期时间列,“mytable”是你的表 SELECT * FROM mytable WHERE date <= DATE_SUB(NOW(), INTERVAL 24 HOUR)

Assuming that "date" is your datetime column and "mytable" is your table SELECT * FROM mytable WHERE date <= DATE_SUB(NOW(), INTERVAL 24 HOUR)

使用您的数据库并在保存时 - 在数据库中查询特定时间段内投票的数量,如果低于您的阈值,请保存。 如果超过阈值,则弹出javascript警报以通知用户该问题。 Use your database and when saving - query the database for the number of votes cast in a specific time period, and if below your threshold, save. If over the threshold, po

...

您可以在表中添加列ts ALTER TABLE core

ADD COLUMN ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

这是一个时间戳,每次插入或更新行时,都会自动设置为机器上的当前时间。 然后你可以做 SELECT id, name, password FROM core

WHERE TIMESTAMPDIFF(HOUR, ts, NOW()) < 24;

它应该返回在过去24小时内添加或更新的

...

STR_TO_DATE( `column_here`, '%l:%i %p' )

在这里阅读更多。 编辑 将'%l:%M %p'为'%l:%i %p' STR_TO_DATE( `column_here`, '%l:%i %p' )

Read more here. EDIT Edited '%l:%M %p' to '%l:%i %p'

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

// create record with the client's ip address and the current dat

...

以下是每10分钟数据获取一行的方法: select date(date_time), hour(date_time), floor(minute(date_time) / 6),

avg(active_energy)

from energy_readings

where date_time >= '2016-11-02' and datetime < '2016-11-03'

group by date(date_time), hour(date_time), floor(minut

...

if (date("G") == '4'){

//give

}elseif (date("G") == '5'){

//takeway

}

或者在查询中 .. WHERE HOUR(NOW()) BETWEEN 4 AND 5

if (date("G") == '4'){

//give

}elseif (date("G") == '5'){

//takeway

}

or in the query .. WHERE HOUR(NOW()) BETWEEN 4 AND 5

如果你正在使用LINUX 使用Cron Jobs # run the script at 4:30am every day

30 4 * * * lynx -dump http://www.yourwebsite.com/myscript.php

如果您的PHP脚本是CLI适合使用 30 4 * * * /usr/local/bin/php /home/me/myscript.php

如果你正在使用WINDOWS 使用sheduled任务http://windows.microsoft.co

...

你可以试试这个: Timer timer = new Timer ();

TimerTask t = new TimerTask () {

@Override

public void run () {

// some code

}

};

timer.schedule (t, 0l, 1000*60*60*24);

否则你可以使用ScheduledExecutorService ExecutorService,可以调度命令在给定延迟后运行,或定期执行。 Y

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值