所以我有一个URL应该在一小时后变为无效。由于某种原因,它从未变为无效。这是代码,
<?php
session_start();
include_once '../db.php';
//DB query
$stmt = $con->prepare("SELECT token_created_at from reset WHERE token = :urltoken");
$stmt->bindValue(':urltoken', $_GET['token']);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while($row = $stmt->fetch()) {
$token_created_at = $row['token_created_at'];
}
$_SESSION['token'] = $_GET['token'];
//Remove after testing
echo $token_created_at."<br>";
$my_dt = DateTime::createFromFormat('m-d-Y H:i:s', $token_created_at);
//Modify error
$expires_at = $my_dt->modify('+1 hour');
//Return current time to match
echo $current_time = date('m-d-Y H:i:s', time());
?>
<?php if($current_time < $expires_at) : ?>
<!DOCTYPE html>
<html>
<body>
<form method="post" action="a.php">
<input type="password" name="pass1" placeholder="Password">
<br>
<input type="password" name="pass2" placeholder="Password, again">
<br>
<input type="submit">
</form>
</body>
</html>
<?php else : ?>
<h1>Link expired</h1>
<?php endif; ?>
有什么想法吗?在数据库中,token
存储为06-27-2014 09:10:50
,当前时间为06-28-2014 09:15:27
,因此应该过期。有任何想法吗?
答案 0 :(得分:2)
您正在将DateTime对象与字符串进行比较。你不能这样做。将它们作为字符串或DateTime对象进行比较。我推荐后者。
$current_time = new DateTime();
<?php if($current_time < $expires_at) : ?>
您也不需要捕获$my_dt->modify('+1 hour');
的结果,因为它会修改$my_dt
。所以你可以这样做:
$my_dt->modify('+1 hour');
$current_time = new DateTime();
<?php if($current_time < $expires_at) : ?>