PHP &会话:有什么办法可以禁用 PHP 会话锁定?

问题描述:

在使用默认会话处理程序时,有什么方法可以禁用 PHP 中的会话锁定吗?

Is there any way to disable the session locking in PHP while using the default session handler?

或者至少有一种方法可以在调用 session_write_close() 后重新启动会话?如果任何输出已经发送到浏览器,session_start() 将不起作用.

Or is there at least a way to restart a session after calling session_write_close()? session_start() does not work if any output is already sent to the browser.

你不想禁用它...如果你这样做,你可能会遇到各种奇怪的问题,当你在一个窗口登录时,在另一个上注销,然后以不一致的状态结束......锁定是有原因的......

You don't want to disable it... If you do, you'll potentially run into all sorts of weird issues where you login on one window, be logged out on another and then wind up in an inconsistent state... The locking is there for a reason...

相反,如果您知道不会在该请求中写入会话,请尽早关闭会话.启动它后,即使在调用 session_write_close 之后,您也可以从它读取整个请求(除非您重新启动它,或做一些其他特殊的事情).因此,您要做的是检查请求以查看它是否是写入请求,如果不是,则在打开它后立即关闭它.请注意,如果您稍后尝试写入会话(用于验证码或 CSRF 保护或其他内容),这可能会产生一些不利影响,因此请谨慎使用...

Instead, close the session very early if you know you are not going to write to it in that request. Once you start it, you'll be able to read from it for the whole request (unless you re-start it, or do some other special things) even after you call session_write_close. So what you would do, is check the request to see if it's a write request, and if it's not, just close it right after opening it. Note that this may have some adverse affects if you later try writing to the session (For Captcha or CSRF protection or something else), so use with caution...

但我不会试图绕过它,而是努力缩短请求长度(以减少锁争用),或者为那些根本不需要会话的请求发出 cookieless 请求......

But instead of trying to get around it, I would put my effort into either shortening the request lengths (to reduce lock contention), or making cookieless requests for those requests that don't need the session at all...