簡體   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