Home > Backend Development > PHP Tutorial > Solution to script blocking problem caused by Session in PHP_PHP Tutorial

Solution to script blocking problem caused by Session in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:34:29
Original
804 people have browsed it


How to solve the session blocking problem: You can avoid this problem by calling session_write_close() after the session operation is completed;


Case 1:

In the process of using session, after opening the session, if the same browser executes the same program, different pages will be locked. This does not happen in different browsers.

Question: Is session_start causing the blocking?

So, I wrote a few pages and tested them, and found that the session caused the blocking, but the other two situations did not cause blocking.

I checked the PHP bug list and found that someone asked this question:

Copy the code The code is as follows:
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 the
session file for exclusive use, and the second request blocks until it
can open it.

PHP’s official reply is:

Copy the code The code is as follows:
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 corruption.

Combined with PHP’s Session mechanism, the cause of the blocking was found. Since PHP's session information is written into files, one client occupies one session file. Therefore, when session_start is called, the file is locked, and it is locked in read-write mode (because the value of the session may need to be modified in the program). In this way, the second call to session_start is blocked. .

The simplest solution:

I checked the PHP manual and found a session_write_close function, which writes session data and end session, that is, writes session data and closes the session at the same time. Therefore, after we are done with the session, we can call this function to close the session file to unlock it. Generally, the session is used to record user identity information so that PHP can perform identity authentication. Therefore, the reading and writing of the session can be placed at the beginning of the page execution. After the execution, the session_write_close function can be called immediately.


Case 2:

The last time I said that I wanted to change opencart, I actually wanted to add a function to opencart to crawl products on Taobao. However, after I finished it, I found a problem, that is, when the script is crawling, because this process is relatively slow, other products at this time The execution of all scripts is blocked until other scripts are fetched and cannot be executed in sequence. After researching for a long time with no results, I asked on Zhihu that it might be a session problem, which needs to be solved by calling session_write_close(). So what is session_write_close() used for? The manual says this:

Copy code The code is as follows:

End the current session and save the session data.

Session data is usually saved after the script is executed without calling session_write_close(). However, in order to protect the session from being written by only one script at any time, the session data will be locked. When using frame web pages and sessions at the same time, you will find that the web pages in the frame will be loaded one by one due to this lock. You can speed up load times by ending the session immediately after all session data modifications have been saved.

This is a good explanation for why my crawling script blocks other pages. Therefore, if you have an ajax request that takes a long time to execute and uses session, you need to call session_write_close() on the server side, otherwise your other pages will be suspended until the request ends! ! !

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/750854.htmlTechArticleHow to solve the session blocking problem: Call session_write_close() after the session operation is completed to avoid this problem; Case 1 : During the use of session, after opening the session, the same browser...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template