php - x分钟后在mysqli数据库和php中删除行
问题描述
我正在用 php 创建一个网站。我有一个名为database1
我有表的数据库cooldown
,my_table
该cooldown
表包含列time
(类型Timestamp
默认值CURRENT_TIMESTAMP
)和ip
(类型 int(30) 默认值none
)。在我的代码末尾,我mysqli_query($conn, "DELETE FROM cooldown WHERE time < NOW() - INTERVAL 5 MINUTE");
应该在 5 分钟后删除该行,但它不会删除该行。这可能是因为用户被重定向所以 php 脚本停止工作?还是我想念别的东西?
代码:
//Test if it is a shared client
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip=$_SERVER['REMOTE_ADDR'];
}
//The value of $ip at this point would look something like: "192.0.34.166"
$ip = ip2long($ip);
//The $ip would now look something like: 1073732954
$query = mysqli_query($conn, "SELECT ip FROM cooldown WHERE ip = '$ip'");
$row = mysqli_fetch_array($query);
if ($row) {
//What page shall the bans be sent to?
header("Location: http://imnothere.epizy.com/cooldown.html"); //cooldownpage
exit();
} else {
$sql1= "SELECT links FROM my_table WHERE Type = 'spotify' ORDER BY RAND() LIMIT 1";
$result1 = $conn->query($sql1); //this actually runs the query on the DB, and comes back with a $result object
if($result1 === false) {
echo $conn->error();
}
$redirect = $result1->fetch_assoc()['links']; //this gets one row from the $result object, and then the 'links' column from that row.
header("Location: " . $redirect);
$sql = "INSERT INTO cooldown(ip, time) VALUES('$ip', NOW())";
$result = $conn->query($sql); //this actually runs the query on the DB, and comes back with a $result object
if($result === false) {
}
mysqli_query($conn, "DELETE FROM cooldown WHERE time < NOW() - INTERVAL 5 MINUTE");
}
解决方案
您的 SQL 查询不会在 5 分钟后删除行,它会删除请求时 5 分钟或更早的所有内容。
您可能最好将该行代码移动到一个单独的文件中,该文件由您运行cron
或在您的数据库中设置一个事件以每分钟左右执行一次。
推荐阅读
- php - 无法在 ubuntu 16.04 上安装 php5.6-zip
- ios - 通过传递环境变量 Xcode 更改应用程序名称
- android - 设置 setOnCompletionListener android studio kotlin 后音频文件仍然循环
- python - Discord.py bot:如何让机器人仅在特定情况下响应命令?
- git - 为什么git在文件移动后完全相同时看到整个文件内容已更改
- c# - 为 .NET Framework 应用程序设置 ElasticAPM 代理
- google-sheets - 生成具有最大值的组合
- python - 计算 FFT 以从 WAV 文件中获取频谱 - 但没有看到预期的答案 .. 使用 Python / Numpy .. 有什么想法吗?
- python - GAN,pytorch 中的自定义批量大小
- nlp - 在 Bert 架构中出现张量错误