PHP Session Locking: How to Prevent Blocking Requests

Today I learned about “PHP Session Locking”:

PHP writes its session data to a file by default. When a request is made to a PHP script that starts the session (session_start()), this session file is locked. What this means is that if your web page makes numerous requests to PHP scripts, for instance, for loading content via Ajax, each request could be locking the session and preventing the other requests from completing.

The other requests will hang on session_start() until the session file is unlocked. This is especially bad if one of your Ajax requests is relatively long-running.

As a developer you can prematurely close the session by calling session_write_close() or – with PHP7 – close it automatically after starting:

    'read_and_close' => true,

PHP Session Locking: How To Prevent Sessions Blocking in PHP requests →
PHP Session Locks – How to Prevent Blocking Requests →

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.