duanniying2342 2016-05-29 17:48
浏览 36
已采纳

如果时间是设置$ _SESSION [“reverify”]

I'm trying to create a script which sets $_SESSION["reverify"] to TRUE when 20 minutes have passed without activity on their account (such as page clicks).

Explanation about the $_SESSION: When $_SESSION["reverify"] is set to TRUE, the user has to reverify him or herself.

I have tried this:

$expire_stamp = date('Y-m-d H:i:s', strtotime("+20 min"));
$now_stamp    = date("Y-m-d H:i:s");

if($now_stamp == $expire_stamp) {
    $_SESSION["reverify"] == true;
}

But it's not a good script at all, and has so many downsides. Am I overthinking, or just being a noob? I can't get this right.


if (isset($_SESSION['loggedin_time']) && (time() - $_SESSION['loggedin_time'] > 120)) {
    $_SESSION["reverify"] = true;
}

if(isset($_SESSION["reverify"]) && $_SESSION["reverify"] = true) {
    header("Location: index.php?reverify=true");
}
  • 写回答

1条回答 默认 最新

  • dongxing8766 2016-05-29 17:56
    关注

    Session timeout with session properties is not so good idea. The problem you are looking to crack has very good explaination at this link here.

    This is answer to another question at stack overflow and is a good read

    Hope this helps

    <?php
    
    function session_timeout_extend($minutes_to_extend){
        session_start();
        $timeInseconds = time() + $minutes_to_extend*60; 
        $_SESSION['last_time_allowed']   =   $timeInseconds;
    }
    
    session_start();
    
    if(!isset($_SESSION['last_time_allowed'])){
       session_timeout_extend(20);
    
        echo 'setting session';
        die();
    }
    
    if(time() >= $_SESSION['last_time_allowed']){
        echo 'session expired';
    }
    else{
      echo 'session active';
    }
    ?>
    

    Please see the code above works . You can test with this code, and thereafter integrate it in your workflow. Please see that this method though will not work after 19 January 2038 refered to as Y2k38 bug.

    You can simply call the method updated above to user verification code block.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算