【问题标题】:Session time out when idle not working php空闲时会话超时不工作php
【发布时间】:2015-02-08 20:17:31
【问题描述】:

我已经编写了一些代码来使会话超时;

<?php

session_start();
// set timeout period in seconds
$inactive = 10;
// check to see if $_SESSION['timeout'] is set
if(isset($_SESSION['timeout']) ) {
$SESSION_life = time() - $_SESSION['timeout'];
if($SESSION_life > $inactive)
{ session_destroy(); header("Location: login.php");exit;  }
}
$_SESSION['timeout'] = time();


if (isset($_SESSION['username'])) {

    echo "<center>Welcome </center>"  ; //     echo "<p> </p>";
    echo " <center>". $_SESSION['username']. "</center>" ;
    echo  "<br /><center>".$_SESSION["role"]."<br /></center>" ;

}else{

    header("location:login.php");

}

但是,如果会话空闲 10 秒,会话不会超时。

【问题讨论】:

  • 在你的重定向后面加上exit;
  • 添加了但还是一样..
  • 如果您修改了代码 - 相应地更改问题。

标签: php session-timeout


【解决方案1】:

看起来你快到了。我会试试这个:

<?php

session_start();

$inactive_time = 10;

if(isset($_SESSION['last_active_time'])){

   $time_since_last_active = time() - $_SESSION['last_active_time'];

   if($time_since_last_active >= $inactive_time){
      // The user has been inactive for too long
      session_destroy();
      header('Location: login.php');
      exit;
   }else{
      // Set the last active tim
      $_SESSION['last_active_time'] = time();
   }

}else{
    $_SESSION['last_active_time'] = time();
}

【讨论】:

  • 对我来说很好 - 你在等 10 秒吗?
  • 是的,我确实等了。我什至将 $inactive_time 更改为 3sec,但它仍然没有重定向到 login.php
  • 那么$time_since_last_active 的值是什么?你没查过吗?
  • 嗯,echo 还是 var_dump()?不知道怎么输出值?
  • 回显但它没有输出任何东西
猜你喜欢
  • 2012-02-17
  • 2014-04-24
  • 2013-06-18
  • 2022-08-17
  • 1970-01-01
  • 2021-03-07
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多