1

所以我有一个应该在一小时后变为无效的 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,因此它应该过期。有任何想法吗?

4

1 回答 1

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) : ?>
于 2014-06-28T13:24:08.557 回答