Analysis of Session blocking Problems and Solutions in PHP programming, and session Blocking
This article describes the Session blocking problem and solutions in PHP programming. We will share this with you for your reference. The details are as follows:
When a session is enabled, different pages are locked when the same program is executed in the same browser. This does not happen in different browsers.
Question:Is session_start blocking?
So I wrote a few pages and tested them. I found that the session caused the blocking, but the other two cases won't cause the blocking.
I checked the PHP Bug list and found someone raised this question:
Description:
------------
Calling session_start () appears to wait until other scripts have exited
That are using the same session. My guess is the 1st request locks
Session file for exclusive use, and the second request blocks until it
Can open it.
PHP official reply:
Thank you for taking the time to write to us, but this is not a bug. This is expected, the session file is locked to avoid upload uption.
Combined with the PHP Session mechanism, we found the cause of blocking. Since the PHP Session information is written into the file, one client occupies one session file. Therefore, when session_start is called, the file is locked in read/write mode (because the session value may need to be modified in the program, it is blocked when session_start is called 2nd times.
Simplest solution:
I checked the PHP manual and found a session_write_close function, which is used to Write session data and end session, that is, Write session data, and close the session. Therefore, we can call this function to close the session file after the session is used up to unlock the lock. Generally, session is used to record user identity information, so that PHP can perform identity authentication. Therefore, the session read and write can be placed at the beginning of the page execution. After the execution, call the session_write_close function immediately.