I ran into this problem trying to fix the $ _SESSION lock behavior.
http://konrness.com/php5/how-to-prevent-blocking-php-requests/
The session file remains locked until the script completes or the session is closed manually.
Thus, by default, the page should open the session in read-only mode. But as soon as it opens read-only, it should be closed and reopened in write mode.
const SESSION_DEFAULT_COOKIE_LIFETIME = 86400; /** * Open _SESSION read-only */ function OpenSessionReadOnly() { session_start([ 'cookie_lifetime' => SESSION_DEFAULT_COOKIE_LIFETIME, 'read_and_close' => true, // READ ACCESS FAST ]); // $_SESSION is now defined. Call WriteSessionValues() to write out values } /** * _SESSION is read-only by default. Call this function to save a new value * call this function like `WriteSessionValues(["username"=>$login_user]);` * to set $_SESSION["username"] * * @param array $values_assoc_array */ function WriteSessionValues($values_assoc_array) { // this is required to close the read-only session and // not get a warning on the next line. session_abort(); // now open the session with write access session_start([ 'cookie_lifetime' => SESSION_DEFAULT_COOKIE_LIFETIME ]); foreach ($values_assoc_array as $key => $value) { $_SESSION[ $key ] = $value; } session_write_close(); // Write session data and end session OpenSessionReadOnly(); // now reopen the session in read-only mode. } OpenSessionReadOnly(); // start the session for this page
Then, when you move on to writing some value:
WriteSessionValues(["username"=>$login_user]);
TrophyGeek Jul 07 '17 at 4:40 2017-07-07 04:40
source share