PHP 简单会话超时
PHP simple session-timeout
我有一个大学项目,我们应该在其中开发一个具有免费会话的静态网站。
我需要一个简单的 php 超时代码。
使用这个正确吗?代码:
<?php
if ($_SESSION['timeout'] + $minutes * 60 < time()) {
// session timed out
} else {
// session ok
}
?>
$_SESSION['timeout']
被设置为 time();
这取决于您的网站逻辑。如果需要,请尝试使用它。
<?php
session_start(); $t=time(); $diff=0; $new=false;
if (isset($_SESSION['time'])){
$t0=$_SESSION['time']; $diff=($t-$t0); // inactivity period
} else {
$new=true;
}
if ($new || ($diff > 10)) { // new or with inactivity period too long
//session_unset(); // Deprecated
$_SESSION=array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) { // PHP using cookies to handle session
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 3600*24, $params["path"],
$params["domain"], $params["secure"], $params["httponly"]);
}
session_destroy(); // destroy session
// redirect client to login page
header('HTTP/1.1 307 temporary redirect');
header('Location: login.php?msg=SessionTimeOut');
exit; // IMPORTANT to avoid further output from the script
} else {
$_SESSION['time']=time(); /* update time */
echo '<html><body>Tempo ultimo accesso aggiornato: ' .$_SESSION['time'].'</body></html>';
}
?>
但我建议使用 session_regenerate_id()
而不是 session_destroy()
我有一个大学项目,我们应该在其中开发一个具有免费会话的静态网站。 我需要一个简单的 php 超时代码。 使用这个正确吗?代码:
<?php
if ($_SESSION['timeout'] + $minutes * 60 < time()) {
// session timed out
} else {
// session ok
}
?>
$_SESSION['timeout']
被设置为 time();
这取决于您的网站逻辑。如果需要,请尝试使用它。
<?php
session_start(); $t=time(); $diff=0; $new=false;
if (isset($_SESSION['time'])){
$t0=$_SESSION['time']; $diff=($t-$t0); // inactivity period
} else {
$new=true;
}
if ($new || ($diff > 10)) { // new or with inactivity period too long
//session_unset(); // Deprecated
$_SESSION=array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) { // PHP using cookies to handle session
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 3600*24, $params["path"],
$params["domain"], $params["secure"], $params["httponly"]);
}
session_destroy(); // destroy session
// redirect client to login page
header('HTTP/1.1 307 temporary redirect');
header('Location: login.php?msg=SessionTimeOut');
exit; // IMPORTANT to avoid further output from the script
} else {
$_SESSION['time']=time(); /* update time */
echo '<html><body>Tempo ultimo accesso aggiornato: ' .$_SESSION['time'].'</body></html>';
}
?>
但我建议使用 session_regenerate_id()
而不是 session_destroy()