繁体   English   中英

设定的时间后,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; ?>

有任何想法吗? 在数据库中, token存储为06-27-2014 09:10:50 ,当前时间为06-28-2014 09:15:27 ,因此token应该过期。 有任何想法吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM