PHP & Sessions: Is there any way to disable PHP session locking?

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…

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…

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…

Leave a Comment