Home  >  Article  >  Backend Development  >  Research on the problem that the page always loads after calling session_start in PHP_PHP Tutorial

Research on the problem that the page always loads after calling session_start in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:57:35968browse

This article will introduce to you a study on the problem that the page always loads after calling session_start in PHP. Friends in need can refer to it. The execution time of a PHP page is relatively long (about 15 seconds), and as long as this page is not completed, other page visits will be loaded for a long time. Only after that page is completed, the remaining pages can be opened. What is going on? After checking, both pages are designed with SESSION operation. The sample code is as follows: Page one: Research on the problem that the page always loads after calling session_start in PHP_PHP Tutorial Page two: Research on the problem that the page always loads after calling session_start in PHP_PHP Tutorial When accessing page one, access page two at the same time. You will find that page two will remain loaded until page one is completed. What is the cause of the problem? The answer is that PHP's SESSION mechanism is causing trouble. PHP will only write the SESSION data to the file after the page code is executed. If the page is not executed, the corresponding SESSION file will always be locked, while other pages will be locked. Accessing this SESSION file can only keep the waiting state, which is why page two needs to wait for page one to be executed before executing it. ​ Now that we know the cause, how should we solve this problem? The answer is simple, PHP has provided relevant interfaces in the kernel: function session_write_close
The description of the official documentation of the function is as follows: Session data is usually stored after your script terminated without the need to call session_write_close(), but as session data is locked to prevent concurrent writes only one script may operate on a session at any time. When using framesets together with sessions you will experience the frames loading one by one due to this locking. You can reduce the time needed to load all the frames by ending the session as soon as all changes to session variables are done. Session data is usually automatically saved after your script ends without calling the session_write_close function. However, in order to prevent data from being written at the same time, the session locks the file to ensure that only one script can access the file at a time. When you use a frame page and access SESSION at the same time, you will encounter a situation where the frame continues to load due to file locking. You can use this function immediately after processing the SESSION variable to reduce the loading time of multiple frames. ​ Like the previous question, we can directly call the session_write_close function to write the session data immediately after operating the SESSION data, so that it will not affect the normal operation of other pages. The code is as follows: Research on the problem that the page always loads after calling session_start in PHP_PHP Tutorial

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632085.htmlTechArticleThis article will introduce to you a study on the problem that the page always loads after calling session_start in PHP. Friends in need can refer to. The execution time of a PHP page is relatively long (about 15 seconds),...
Statement:
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