URL没有在设定的时间后过期


URL doesn't expire after set time

所以我有一个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; ?>

任何想法?在DB中,token存储为06-27-2014 09:10:50,当前时间为06-28-2014 09:15:27,因此应该过期。什么好主意吗?

您正在将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) : ?>