PHP session_start () slow problem analysis and solution

Source: Internet
Author: User


I found a problem when I was doing something recently.

When an interface crashes and the access address is tested, the browser remains in the waiting status.

You can only restart the web server.

If you do not restart the web server for code debugging, you will not be able to continue when session_start () is found.

The cause of the problem was finally understood:

Because the request sent by the browser is processed by creating a thread on the web server, the session is stored on the server.

The browser is waiting for a response, indicating that the web server is processing your request. However, due to the long response timeout, you may have performed the next debugging.

Before the previous request is killed, it occupies your session resources, and the session resources cannot be shared. The requests you send again will always be waiting for the session resources to be obtained.

That is to say, you can get the session resources and parse the code until the last request ends.


Previously, it was found that the PHP page was slow to call the session_start () method, and even took dozens of seconds. Now I finally found the cause.

Solution

Because the session uses files, multiple requests will lock the Session file and try to release the Session as soon as possible when no Session is needed.
UseSession_write_close () method

For example

The code is as follows:

Session_start (); // starts the session
$ _ SESSION ['user'] = "Me ";
Session_write_close (); // close write capability
Echo $ _ SESSION ['user']; // you can still access it

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.