PHP 8.3.22 Released!

Voting

: min(eight, nine)?
(Example: nine)

The Note You're Voting On

jcastromail at yahoo dot es
7 years ago
Why this function is highly important? I explain.

In a nutshell, how session works:
Client side: php is sending back to the client, a cookie with the id of the session. So, the session ends when the server ends to process the script and not when session_write_close() is executed. So, using session_write_close() for fast saving the session in the client side is useless unless you are ob_flush() and flush() to the customer.

Server side: It could be changed but the normal behavior is to save the session information in a file. For example:

sess_b2dbfc9ddd789d66da84bf57a62e2000 file

**This file is usually locked**, so if two sessions are trying open at the same time, then one is freezed until the file is unlocked. session_write_close() ends the lock.

For example:
<?php
$t1
=microtime(true);
session_start();
sleep(3);
session_write_close();
$t2=microtime(true);
echo
$t2-$t1;
?>

If we run this code in two processes (using the same session, such as two tabs), then one will return 3 seconds while the other will return 6 seconds.

Its caused because the first process lock the session file.

However, changing to:
<?php
$t1
=microtime(true);
session_start();
session_write_close();
sleep(3);
$t2=microtime(true);
echo
$t2-$t1;
?>
both files runs at 3 seconds.

For PHP 7.0 and higher, we could use session_start(true); for auto close after the first read.

This operation is highly important for AJAX when we used to do many operations in parallel by using the the same session

<< Back to user notes page

To Top