【问题标题】:setting up Session Timeout PHP设置会话超时 PHP
【发布时间】:2015-10-29 11:31:45
【问题描述】:

我还是 PHP 语言的新手。我想设置会话超时以确保当用户登录到他们的帐户时,当用户登录时间过长时,帐户会自动注销之前的几分钟/ 1 小时。我参考了这个链接

http://bytes.com/topic/php/insights/889606-setting-timeout-php-sessions

在设置时。我可能不确定我是否正确放置了代码。但我希望有人能指导我解决这个问题。

我只是在其中一个页面中放置会话超时来进行测试。会议在 1 分钟后结束。

coupon.php

<?php 
session_start();
$timeout = 60; // Number of seconds until it times out.

// Check if the timeout field exists.
if(isset($_SESSION['timeout'])) {
    // See if the number of seconds since the last
    // visit is larger than the timeout period.
    $duration = time() - (int)$_SESSION['timeout'];
    if($duration > $timeout) {
        // Destroy the session and restart it.
        session_destroy();
        session_start();
    }
}

// Update the timeout field with the current time.
$_SESSION['timeout'] = time();
?>

sessionTimeout.php

<?php
/***
 * Starts a session with a specific timeout and a specific GC probability.
 * @param int $timeout The number of seconds until it should time out.
 * @param int $probability The probablity, in int percentage, that the garbage 
 *        collection routine will be triggered right now.
 * @param strint $cookie_domain The domain path for the cookie.
 */
function session_start_timeout($timeout=5, $probability=100, $cookie_domain='/') {
    // Set the max lifetime
    ini_set("session.gc_maxlifetime", $timeout);

    // Set the session cookie to timout
    ini_set("session.cookie_lifetime", $timeout);

    // Change the save path. Sessions stored in teh same path
    // all share the same lifetime; the lowest lifetime will be
    // used for all. Therefore, for this to work, the session
    // must be stored in a directory where only sessions sharing
    // it's lifetime are. Best to just dynamically create on.
    $seperator = strstr(strtoupper(substr(PHP_OS, 0, 3)), "WIN") ? "\\" : "/";
    $path = ini_get("session.save_path") . $seperator . "session_" . $timeout . "sec";
    if(!file_exists($path)) {
        if(!mkdir($path, 600)) {
            trigger_error("Failed to create session save path directory '$path'. Check permissions.", E_USER_ERROR);
        }
    }
    ini_set("session.save_path", $path);

    // Set the chance to trigger the garbage collection.
    ini_set("session.gc_probability", $probability);
    ini_set("session.gc_divisor", 100); // Should always be 100

    // Start the session!
    session_start_timeout(60, 10);

    // Renew the time left until this session times out.
    // If you skip this, the session will time out based
    // on the time when it was created, rather than when
    // it was last used.
    if(isset($_COOKIE[session_name()])) {
        setcookie(session_name(), $_COOKIE[session_name()], time() + $timeout, $cookie_domain);
    }
}

?>

index.php

<?php
if(!isset($_SESSION))
{ 
  session_start(); 
}

$timeout = $_SERVER[‘REQUEST_TIME’];
/**
 * for a 1 minute timeout, specified in seconds
*/
$timeout_duration = 60;

/**
 * Here we look for the user’s LAST_ACTIVITY timestamp. If
 * it’s set and indicates our $timeout_duration has passed,
 * blow away any previous $_SESSION data and start a new one.
 */
if (isset($_SESSION[‘LAST_ACTIVITY’]) && ($timeout - $_SESSION[‘LAST_ACTIVITY’]) > $timeout_duration) {
    session_unset();
    session_destroy();
    session_start();
}

/**
 * Finally, update LAST_ACTIVITY so that our timeout
 * is based on it and not the user’s login time.
 */
$_SESSION[‘LAST_ACTIVITY’] = $timeout;
?>

【问题讨论】:

  • 你为什么使用 smart 引号?
  • 什么意思? @Script47
  • 查看下面@Fred-ii- 的答案。
  • 在 ms word 中停止编程
  • @SitiNurainiYakob 我注意到你发布了很多问题。要将问题标记为已解决,应正确关闭它。访问meta.stackexchange.com/questions/5234/…,然后对提供解决方案的所有答案执行相同操作。这告诉大家找到了解决方案。这有助于告知人们问题已解决。否则,它将保留在 Stack 上的未答复类别中。

标签: php session session-cookies logout session-timeout


【解决方案1】:

你的代码中有一大堆时髦的引号,这会导致它失败。

即:

$_SERVER[‘REQUEST_TIME’];
         ^            ^

那些应该是常规/标准引号。

$_SERVER['REQUEST_TIME'];

对其余部分进行这些更改。

使用错误报告会发出通知。

您可以在代码编辑器甚至记事本中使用 CTRL-H 轻松查找和替换它们

【讨论】:

  • Arggggggggg 海盗弗雷德无所不知
  • Arrrrrr 伙计! @Dagon
  • 看来帮派都在这里了 ;-)(詹姆斯帮)
  • @Script47 你必须看完十个小时
  • 是的,@Dagon 和我会在公海的某个地方处理一些事务。一对小鸡在岛上等着我们。现在要记住它是哪一个。
猜你喜欢
  • 2011-06-03
  • 1970-01-01
  • 2016-01-29
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多