Research on the problem of page loading after PHP calls session_start

Source: Internet
Author: User
The execution time of a PHP page is relatively long (about 15 seconds). As long as the page has not been executed, other page accesses are in the loading status for a long time, and only the page has been executed, the remaining pages can be opened.
What is the situation? After checking, the two pages are designed for SESSION operations. The sample code is as follows:
Page 1:
Page 2:
 
When you access page 2 at the same time, you will find that page 2 remains loaded until the execution of page 1 is complete.
What is the cause of the problem?
The answer is that PHP's SESSION mechanism is at odds. PHP will write SESSION data to the file only after the page code is executed, and the page will not be executed, the corresponding SESSION files are always locked, while other pages have to wait for access to this SESSION file, this is why page 2 is executed only after Page 1 is executed.
 
After knowing the cause, how should we solve this problem?
The answer is simple. PHP already provides related interfaces in the kernel: functions.Session_write_close
The official function documentation is described 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 automatically saved after your script ends without calling the session_write_close function. However, 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 access the SESSION on the Framework page at the same time, the framework will be continuously loaded due to file locking. You can use this function immediately after processing the SESSION variable to reduce the loading time of multiple frameworks.
 
As in the previous issue, we can directly call the session_write_close function to write SESSION data immediately after the session data is operated, so that the normal operation of other pages will not be affected. The code is as follows:
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.