Solution to Session blocking problem in php

黄舟
Release: 2023-03-14 19:00:02
Original
1340 people have browsed it

This article mainly introduces the problem and solution of Session blocking in PHP programming. It briefly analyzes the principles, reasons and simple solutions of PHP session blocking. Friends in need can refer to the following

Examples of this article Learn about Session blocking problems and solutions in PHP programming. Share it with everyone for your reference, the details are as follows:

During the use of session, after opening the session, if the same browser executes the same program, different pages will be locked. This does not happen in different browsers.

Question: Is session_start causing the blocking?

So, I wrote a few pages and tested them, and found that the session caused the blocking, but the other two situations did not cause blocking.

I checked the PHP bug list and found that 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 the
session file for exclusive use, and the second request blocks until it
can open it.

PHP’s official reply is:

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 corruption.

Combined with PHP's Session mechanism, the cause of the blocking was found. Since PHP's Session information is written into files, one client occupies one session file. Therefore, when session_start is called, the file is locked, and it is locked in read-write mode (because the value of the session may need to be modified in the program). In this way, the second call to session_start is blocked. .

The simplest solution:

I checked the PHP manual and found a session_write_close function, which writes session data and end session, that is, writes session data and close the session at the same time. Therefore, after we are done with the session, we can call this function to close the session file to unlock it. Generally, the session is used to record user identity information so that PHP can perform identity authentication. Therefore, the reading and writing of the session can be placed at the beginning of the page execution. After the execution, the session_write_close function can be called immediately.

The above is the detailed content of Solution to Session blocking problem in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!