2014-10-10 39 views
1

我有一个在线系统,需要用户闲置60分钟后注销。从时间删除一个小时()

现在,当前的代码可以计算出什么时间少了30分钟。

$cut = time() - 30*60; 

现在我会想,以下将删除一个小时,所以超时是60分钟。

$cut = time() - 60*60; 

这是发生的完整代码。在不活动60分钟后,我需要该人员注销。

// Time out Users 60 minutes 
    $last = $_SESSION['time']; 
    $cut = time() - 60*60; 

    if ($last < $cut) 
     { 
      session_destroy(); 

      $get = $_GET; 
      $location = 'login.php?timeout=true'; 
      foreach($get as $key => $item) 
       { 
        $location .= '&'.$key."=".$item; 
       } 
      header('Location: '.$location); 
     } 
    else 
    $_SESSION['time'] = time(); 

现在,代码不起作用,我不认为。

+0

Mayby这可以帮助你如何设置会话的生命周期! http://stackoverflow.com/q/6360093/3933332 – Rizier123 2014-10-10 13:10:03

+0

你需要该人被注销(即:会议被销毁)。你需要'?timeout = true'吗?如果没有,请将最大会话生存期设置为3600 - http://php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime – 2014-10-10 13:10:05

+0

我认为您需要了解会话处理程序类。 http://php.net/manual/en/sessionhandler.destroy.php – Naeem 2014-10-10 13:11:20

回答

1

使用的strtotime()代替它可以来得心应手

声明这样的会议,每次用户进行REQ。

$_SESSION['time'] = strtotime("now"); 

编码部分

$last = $_SESSION['time']; 

$cut = strtotime("-60 min"); 


if ($last < $cut) //can also use strtotime("-60 min") directly 
    { 
     // session_destroy(); 
     unset($_SESSION['time']); //use inorder to delete only that session 


     $get = $_GET; 
     $location = 'login.php?timeout=true'; 
     foreach($get as $key => $item) 
      { 
       $location .= '&'.$key."=".$item; 
      } 
     header('Location: '.$location); 
    } 
else 
$_SESSION['time'] = strtotime("now"); 

确保会议在每个页面开始。

工作正常...

1

如果你只是想看看它已经一个多小时,因为$_SESSION['time']已经过期更多,只需添加一个小时到了那个时候,现在把它比作。如果它比现在少一个小时以前。

$now = new DateTime(); 
$last = new DateTime('@' . $_SESSION['time']); 
$last->modify('+1 hour'); 
if ($last < $now) { 

} 

或者从现在减去一个小时,看看它是否仍然大于$_SESSION['time']

$now = new DateTime(); 
$now->modify('+1 hour'); 
$last = new DateTime('@' . $_SESSION['time']); 
if ($last < $now) { 

} 
0

你需要时间给出如下

$cut = time(); 
$lesstime = date('YOUR TIME FORMATE',strtotime($cut,"-1 hours")); 
0

与下面的代码尝试转换:

$cut = time() - 60*60; 

低于

$cut = strtotime('-1 hour');