【发布时间】:2015-10-27 20:21:54
【问题描述】:
我查看了几个之前回答的问题,但这些解决方案似乎对我不起作用。
所以我有一个简单的登录脚本,如下所示:
login.php
// If page requires SSL, and we're not in SSL mode,
// redirect to the SSL version of the page
if($_SERVER['SERVER_PORT'] != 443) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
exit();
}
// put sha1() encrypted password here
$password = 'sha1passwordgoeshere';
session_start();
if (!isset($_SESSION['loggedIn'])) {
$_SESSION['loggedIn'] = false;
}
if (isset($_POST['password'])) {
if (sha1($_POST['password']) == $password) {
$_SESSION['loggedIn'] = true;
} else {
$logFailure = true;
}
}
if (!$_SESSION['loggedIn']):
// Load Login Page
exit();
endif;
注销.php
// If page requires SSL, and we're not in SSL mode,
// redirect to the SSL version of the page
if($_SERVER['SERVER_PORT'] != 443) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
exit();
}
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();
// Unset all of the session variables.
$_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")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// Finally, destroy the session.
session_destroy();
我的其余页面在文件顶部包含 login.php。
大部分时间似乎一切正常。但是,自从我转移到新服务器并使用 php 5.6.14 后,我一直注意到我不时会超时。
例如,现在我可以使用 Internet Explorer 登录,但不能使用 Firefox 或 Chrome。如果我清除 cookie,我就可以使用 Firefox 和 Internet Explorer 登录,但不能使用 Chrome。
更新
我可以通过成功登录,立即退出,然后再次登录来立即导致超时。第二次登录时,超时。
这真的让我很沮丧,而且我不是会话大师,所以我不明白它为什么会这样,也不明白如何修复它以使其不那么微妙。
我只使用会话来记录登录,而不是用于其他任何事情。我确实在网站的某些页面上使用 AJAX,但并不经常。
基本上,我不希望它超时。我将如何防止这些超时发生?
【问题讨论】:
-
如果您只记录登录,Cookie 会起作用吗?
-
当然,但我认为我已经通过会话使用了 cookie?
-
创建了一个 cookie。但是您的脚本并没有专门针对它。由于会话必须有超时,它最终总会死掉。您可以创建一个 cookie 以延长生存时间。脚本可以查看您创建的 cookie 是否存在而不是实时会话。这不是一个好的做法,但可以防止您遇到的问题。
-
我明白了。我的代码有什么问题吗?这在我移动服务器之前没有发生,所以我不知道是 php 版本更新导致问题还是 Apache 配置
-
当你说第二次登录时超时,你怎么知道的?