PHPsession_start () slow problem analysis and solution

Source: Internet
Author: User
If you do not restart the web server for code debugging, you will not be able to continue until session_start (). The cause of the problem is finally understood: because the request sent by the browser is still processed by a thread established on the web server, because the session is

Recently, when I was doing something, I found a problem. When an interface crashes and the access address is tested, the browser is always waiting for a response, only restart the web server.

If you do not restart the web server for code debugging, you will not be able to continue until session_start (). The cause of the problem is finally understood: because the requests sent by the browser are processed by creating threads 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 normally and enter the code parsing only when the last request ends. it was found that the PHP page was very slow to call the session_start () method, and even took dozens of seconds, the cause is finally found.

Solution: because the session uses a file, multiple requests will lock the Session file. try to release the Session as soon as possible when no Session is needed. use the session_write_close () method, for example:

  1. Session_start (); // starts the session
  2. $ _ SESSION ['user'] = "Me ";
  3. Session_write_close (); // close write capability
  4. 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.