Home >Backend Development >PHP Tutorial >Solve the problem of AJAX request making PHP response time too long

Solve the problem of AJAX request making PHP response time too long

WBOY
WBOYOriginal
2016-08-08 09:22:531380browse

Now we have developed many applications that rely on Ajax requests. In some cases, even entire pages rely on Ajax. Sometimes we will notice that when a web page sends two or more Ajax requests, PHP's response time will be very long and the response content will be returned at the same time.

This problem is most likely caused by the way you handle PHP sessions. Follow this article to understand this problem and do something to avoid it.

Content

What is a PHP session?
What is Ajax?
Specific problem
Cause
Solution to the problem
Summary

What is a PHP session?

In order 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 visited all pages each time without logging in. In this case, you can use cookies or sessions.
As you may have realized, sessions are a way of storing user information that can be retrieved on any page. Unlike Cookies, Sessions are stored on the server, and no user can directly change this information.
By default, Sessions are valid until the user closes the browser, or expires after the user has been inactive for a period of time specified in the PHP configuration file.
In a PHP page, whenever you want to store or retrieve user data, you must call session_start() at the beginning of the page, so you have permission to use $_SESSION to obtain session data.

What is Ajax?

Ajax stands for Asynchronous JavaScript and XML, it is a way to send data to and receive data from the server without reloading the entire page.
We use this way to send data and retrieve it from the server faster. We don't have to get the entire page and render it in the browser because that's slow.
Therefore, we can update a portion of a page and have the change visible to the user, like a user scrolling down a Facebook timeline page to see what they want to see, as new content is added without having to reload the entire page.

Specific questions

Developing web applications that are almost 100% based on Ajax is nothing new, but when a web page sends two or more Ajax requests at the same time, you will notice that the request will take a long time, and The request was completed almost at the same time.

The reason

When you want the server to send an Ajax request, the PHP script also opens 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, so 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 will first use session_start() in the main file. Therefore, if you are using a framework or library that calls session_start(), it will cause a session file lock, which will delay simultaneous Ajax requests to the same user using the same browser.

Solution to the problem

Calling the session_write_close() function will cause PHP to write the session file and close it, so after the session file is released, another request will have permission to write.
After calling session_write_close(), the current script will continue to run normally, but you should know that no session variables are allowed to be changed after calling session_write_close(); in the same script, other requests sent to PHP at the same time can lock the session file and change session variables.
To allow you to see this kind of problem, I created 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, then open the browser console to view the request and response times.
As we see in the sample code in this file, if we create multiple requests like the following code...

<code><span>session_start()</span>;
<span>sleep(<span>5</span>)</span>;</code>

Each request for the same user will wait until the previous request is completed before it is completed. It will take 5 seconds because the session file is not released until the script is completed. Therefore, when session_start() is called for the first time, new requests will be blocked. That would kill the idea of ​​asynchronous requests, that is, multiple requests being sent and executed at the same time.
If you change the code in the file:

<code><span>session_start()</span>;
<span>// do something useful here</span><span>session_write_close()</span>;
<span>sleep(<span>5</span>)</span>;</code>

The third line of code will release 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 and will make you worry about why strange things are happening. But once you understand how things work, everything makes sense and you can think better about solving problems.

Translation source: http://www.ido321.com/1577.html

This article is translated based on @Eslam Mahmoud's "Fix the AJAX Requests that Make PHP Take Too Long to Respond". The entire translation contains my own understanding and thoughts. If the translation is not good or there is something wrong, please ask a friend who works in the same field. Guidance. If you want to reprint this translation, please 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 introduces how to solve the problem of AJAX request causing PHP to take too long to respond, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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