Analysis of Session blocking Problems and Solutions in PHP programming, and session Blocking

Source: Internet
Author: User
Tags php session

Analysis of Session blocking Problems and Solutions in PHP programming, and session Blocking

This article describes the Session blocking problem and solutions in PHP programming. We will share this with you for your reference. The details are as follows:

When a session is enabled, different pages are locked when the same program is executed in the same browser. This does not happen in different browsers.

Question:Is session_start blocking?

So I wrote a few pages and tested them. I found that the session caused the blocking, but the other two cases won't cause the blocking.

I checked the PHP Bug list and found someone raised this question:

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
Session file for exclusive use, and the second request blocks until it
Can open it.

PHP official reply:

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 upload uption.

Combined with the PHP Session mechanism, we found the cause of blocking. Since the PHP Session information is written into the file, one client occupies one session file. Therefore, when session_start is called, the file is locked in read/write mode (because the session value may need to be modified in the program, it is blocked when session_start is called 2nd times.

Simplest solution:

I checked the PHP manual and found a session_write_close function, which is used to Write session data and end session, that is, Write session data, and close the session. Therefore, we can call this function to close the session file after the session is used up to unlock the lock. Generally, session is used to record user identity information, so that PHP can perform identity authentication. Therefore, the session read and write can be placed at the beginning of the page execution. After the execution, call the session_write_close function immediately.

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.