-2

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

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

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

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$con=mysqli_connect("domain.com.mysql","domain_com","domain_password","domain_database");
$result = mysqli_query($con,"SELECT * FROM ipblock WHERE ip='".$ip."'");
while($row = mysqli_fetch_array($result));

if($ip == $row['ip']) //and code for checking how old the record is
{
// The user has used the function within 24 hours, kill the script.
echo "Come back in 24 hours";
exit;
}
else
{
// Looks clear, let them use the function
$MyFunction = true;
}
?>

我迷路了,如您所见,我还遗漏了一些删除旧记录的语句(-24 小时)..

谁能给我一个如何做到这一点的例子?谢谢

4

2 回答 2

0

我将创建一个带有函数加载存储 IP 地址和时间戳的表当用户加载页面时,在运行该函数之前,选择 IP 地址和时间戳 < 24 小时前。如果您运行该功能:存储用户 IP

无需删除旧记录。但是您可以将其用于验证功能:在调用该功能时删除所有超过 24 小时的事件。或者设置一个 cronjob 每 12 小时运行一次,以删除所有较旧的函数加载记录

基本上,您可以在数据库中创建一个时间戳字段。然后使用 timediff() 将存储的值与 now() 的值进行比较。

于 2013-10-15T10:39:11.183 回答
0

Firstly store the IP and Access time as a pair in a table. Then you check is simple to see if there are any records in existence where the IP matches and the timestamp is less than 24 hours ago.

Before the function runs, complete an INSERT ... ON DUPLICATE UPDATE, and have the IP address as the primary key in the table.

The removal of old entries is then less of a priority and you can schedule this to be whenever convinient and remove entries where the access time is greater than 24 hours ago.

Store the access time as a myqsl date time, and do the comparison using the where clause:

WHERE accesstime >= DATE_SUB(NOW(), INTERVAL 1 DAY)
于 2013-10-15T11:02:11.380 回答