Session Locking

suggest change

With the default files handler, PHP writes session data into a file on the server. When a request calls session_start(), PHP locks that session file. Another request with the same session id waits in session_start() until the lock is released.

The lock is held until the script finishes or the session is closed. To keep later requests from waiting, close the session after writing:

// php < 7.0 
// start session 
session_start();

// write data to session
$_SESSION['id'] = 123; // session file is locked, so other requests are blocked

// close the session, release lock
session_write_close();

After session_write_close(), $_SESSION is still readable in this request.

echo $_SESSION['id'];    // will output  123

In PHP 7.0 and later, session.lazy_write (on by default) skips rewriting the session file when the data did not change. The lock is still held until the script ends or session_write_close() runs. Passing read_and_close to session_start() reads the session and releases the lock immediately; later writes to $_SESSION are not saved.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents