我还是PHP语言的新手。我想设置会话超时,以确保当用户登录到他们的帐户时,在用户登录太长时,帐户自动注销前将限制为几分钟/ 1小时。我参考了这个链接
设置时http://bytes.com/topic/php/insights/889606-setting-timeout-php-sessions
无论我是否正确放置代码,我可能都不确定它是如何工作的。但我希望有人能指导我完成这个问题。
我只是在一个页面中放置会话超时来测试。会议在1分钟后结束。
coupon.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);
}
}
?>
sessionTimeout.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;
?>
的index.php
{{1}}
答案 0 :(得分:2)
你的代码中有一大堆时髦的引号会导致它失败。
即:
$_SERVER[‘REQUEST_TIME’];
^ ^
那些应该是常规/标准报价。
$_SERVER['REQUEST_TIME'];
对其他人进行更改。
使用错误报告会发出通知。
您可以使用CTRL-H
在代码编辑器甚至记事本中轻松查找和替换这些内容