: This article mainly introduces how to solve the problem that AJAX requests cause PHP to respond for a long time. if you are interested in PHP tutorials, refer to it. Now we have developed many applications that depend on Ajax requests. in some cases, even all pages depend on Ajax. Sometimes we notice that when a webpage sends two or more Ajax requests, PHP will take a long time to respond and return the response content at the same time.
This problem is probably caused by the way you handle PHP sessions. follow the instructions in this article to understand the problem and handle it to avoid it.
Content
What is a PHP session? What is Ajax? Summary of specific causes and solutions
What is a PHP session?
To understand this problem, it is necessary to first understand PHP sessions and Ajax, and how they interfere.
Suppose you are developing a Web application and want to identify different users. You want to remember who browsed all pages but didn't log on. in this case, you can use cookies or sessions.
You can realize that sessions is a way to store user information. it can retrieve user information on any page. Unlike Cookies, Sessions is stored on the server and cannot be directly changed by all users.
By default, Sessions is valid before the user closes the browser, or expires after the user does not have activity within the time specified in the PHP configuration file.
On the PHP page, no matter when you want to store or retrieve user data, you must call session_start () at the beginning of the page.$ _ SESSIONTo obtain session data.
What is Ajax?
Ajax stands for Asynchronous JavaScript and XML, which is a way to send data to and receive data from the server without reloading the entire page.
We use this method to send data and retrieve data from the server at a fast speed. We don't need to get the whole page and render it in the browser, because it is very slow.
Therefore, we can update a part of the web page, and users can see this change, just as users can scroll down the Facebook Timeline page to see what they want to see, as new content is added, the entire page does not need to be reloaded.
Question
Developing almost 100% Ajax-based Web applications is nothing new, but when a Web page sends two or more Ajax requests at the same time, you will notice that the request takes a long time, the request is completed almost at the same time.
Cause
When you want the server to send an Ajax request, the PHP script also enables session_start (), and its call will lock the PHP session file.
As you may already know, PHP stores session data in files on the server by default. Because only one PHP request can change the same session file, two simultaneous PHP requests may cause a typical file lock condition. therefore, any other session_start () request called by PHP for the same user will have to wait until the first request ends.
Now, most PHP frameworks use session_start () in the main file (). Therefore, if you are using a framework or function library that will call session_start (), it will cause session file locks. for users using the same browser, this will delay the simultaneous sending of Ajax requests.
Solution
CallSession_write_close ()The function will write PHP to the session file and close it. Therefore, after the session file is released, another request will have the permission to write data.
CallSession_write_close ()The current script will continue to run normally, but you should be clear thatSession_write_close ()In the same script, other requests sent to PHP can lock the session file and change the session variable.
To help you see this problem, I created a test code and uploaded it to github. You can find the test script here. Locally, you need to use an instance to use the test code, and then open the browser console to view the request and response time.
As we can see in this file, if we create multiple requests like the following code...
session_start();sleep(5);
Each request of the same user will wait until the previous request is completed. It will take 5 s, because the session files are not released until the script is completed. Therefore, when session_start () is called for the first time, new requests will be blocked. The idea of killing asynchronous requests is that multiple requests will be sent and executed at the same time.
If you change the code in the file:
session_start();// do something useful heresession_write_close();sleep(5);
The third line of code releases the session file lock, so another concurrent request can run without waiting, because it can call session_start () without any problems.
Summary
PHP is a bit subtle, which worries you about why strange things happen. However, once you understand how things run, everything will become meaningful and you can better think about how to solve the problem.
Http://www.ido321.com/1577.html.
According to @ Eslam Mahmoud's "Fix the AJAX Requests that Make PHP Take Too Long to Respond", this article carries my own understanding and ideas, if the translation is poor or there is something wrong, ask your colleagues for advice. If you need to reprint this translation, need to indicate the English source: http://www.phpclasses.org/blog/post/277-Fix-the-AJAX-Requests-that-Make-PHP-Take-Too-Long-to-Respond.html
The above describes how to solve the problem that AJAX requests cause PHP to respond for too long, including some content, and hope to help friends who are interested in PHP tutorials.