设置会话超时PHP


setting up Session Timeout PHP

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

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

设置时。我可能不确定它是如何工作的,我是否正确地放置了代码。但我希望有人能引导我解决这个问题。

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

优惠券.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;
?>

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

即:

$_SERVER[‘REQUEST_TIME’];
         ^            ^

这些应该是常规/标准报价。

$_SERVER['REQUEST_TIME'];

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

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

  • http://php.net/manual/en/function.error-reporting.php

您可以在代码编辑器中,甚至在记事本上用CTRL-H 轻松找到并替换它们