目标:确定是否已经过了十分钟 设置:MySQL数据库有一个名为codeExpireDate的字段,它是一个日期时间字段。当用户在我的网站上执行操作时,会更新codeExpireDate。理想情况下,下面的逻辑应该计算是否已经过了10分钟。
我在我的数据库中看到,每次执行操作时codeExpireDate确实在改变,所以这不是永不更新的时间问题,它似乎是确定时间量是否已经过去的逻辑部分。奇怪的是,在将文件的位置从一个文件移动到另一个文件之前,用于工作的代码很好但是没有其他任何改变了。?
我有以下代码(所有帮助非常感谢):
在recover.php中:
//fun code stuff before this line..
$resetSQL = "UPDATE users SET tempCode = '".$rand."', codeExpireDate = sysdate() + INTERVAL 10 MINUTE WHERE email = '".$email."'";
//some more magical code after this line..
在reset.php中:
//....
$timeSQL = "SELECT codeExpireDate, sysdate() currTime, tempCode FROM users WHERE token = '".$token."'";
$timeResult = mysqli_query($db,$timeSQL);
$timeRow = $timeResult->fetch_assoc();
$codeExpireDate = $timeRow["codeExpireDate"];
$currTime = $timeRow["currTime"];
$tempCodeFromDB = $timeRow["tempCode"];
$currDateTime = new DateTime($currTime);
$expireDateTime = new DateTime($codeExpireDate);
if ($currDateTime > $expireDateTime){
echo "<script> alert('It has been longer than 10 minutes since you requested to reset your password and your code has expired. Please request to reset it again.');
window.location='recover.php';
</script>";
} else {
....