简体   繁体   English

如果服务器闲置,php会话将在第一次尝试时失败

[英]php session fails on first attempt if server has been idle

For some reason the first login attempt will fail if the server has been idle for some time, for example a couple of hours. 由于某种原因,如果服务器闲置了一段时间(例如几个小时),则首次登录尝试将失败。

This is what happens: 这是发生了什么:

  1. The user navigates to a login script 用户导航到登录脚本
  2. The script sets session variables and redirects to the main page 该脚本设置会话变量并重定向到主页
  3. The main page should be able to read the session variable (but fails in this case) 主页应该能够读取会话变量(但是在这种情况下会失败)

There are no remarks in the PHP error log and the second attempt works fine. PHP错误日志中没有任何注释,第二次尝试可以正常进行。 It only happens for the first user accessing the page after a idle period. 只有在闲置时间之后,第一个访问页面的用户才会发生这种情况。 For example if another user tries to log in 5 seconds later, everything works fine. 例如,如果另一个用户尝试在5秒后登录,则一切正常。

This is on a IIS7 with PHP 5.3 using FastCGI. 这是在使用FastCGI的IIS7和PHP 5.3上。

Any ideas what's wrong here or what I can do to find a solution? 有什么想法在这里有什么问题,或者我可以找到解决方案吗?

This is roughly how the scripts are set up login.php: 大致是通过login.php设置脚本的方式:

@session_start();
$_SESSION['userid'] = $userid;
session_write_close();
session_unset();
usleep(200000);     
header('Location:index.php');

main script 主脚本

session_start();
if (isset($_SESSION['userid'])) {
   // Login successful
}

Remove the three lines. 删除三行。

session_write_close();
session_unset();
sleep(200000); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM