Solve the problem that AJAX requests make PHP response time too long

Source: Internet
Author: User
Tags php session
Now we have developed many applications that rely on AJAX requests, and in some cases, even all of them rely on Ajax. Sometimes we notice that when a Web page sends two or more AJAX requests, the response time of PHP is very long and the response content is returned in conjunction.

This problem is probably caused by the way you handle PHP sessions, follow this article to understand the problem, and do some processing to avoid this problem.

Content

What is a PHP session?

What is Ajax?

Specific questions

Cause of

How to solve the problem

Summarize

What is a PHP session?

To understand this problem, it is necessary to understand the PHP session and Ajax, and how they interfere.
Suppose you are developing a Web application that wants to identify different users. You want to remember who each time you browse all the pages but are not logged in, in which case you can use cookies or sessions.
As you can already realize, sessions is a way to store user information that can be retrieved from any page again. Unlike cookies, sessions is stored on the server, and all users cannot directly change the information.
By default, sessions is valid until the user closes the browser, or the user does not expire within the time specified in the PHP configuration file.
In the 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 get SESSION data.

What is Ajax?

Ajax represents asynchronous JavaScript and XML, which is a way to send data to the server 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 faster rate. We don't have to get the whole page and render it in the browser because it's very slow.
So we can update part of the page, and the user can see the change, just as the user scrolls down the Facebook timeline page to see what they want to see, as new content is added without overloading the entire page.

Specific questions

Developing almost 100% AJAX-based Web applications is no longer a novelty, but when a Web page sends two or more AJAX requests at the same time, you'll notice that the request takes a long time and the request is completed almost at the same time.

Cause of

When you want the server to send an AJAX request, the PHP script also turns on Session_Start (), and its call locks the PHP session file.
As you probably already know, PHP defaults to storing session data in files on the server. Because only one PHP request can change the same session file, two simultaneous PHP requests can cause a typical file lock condition, so any other session_start () request that is 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 invokes Session_Start (), it will cause session file locks, which will delay simultaneous AJAX requests for the same user with the same browser.

How to solve the problem

Calling the session_write_close () function causes PHP to write to the session file and close it, so after releasing the session file, another request has permission to write.
After calling Session_write_close () , the current script will continue to function normally, but you should be aware that it is not allowed to change any session variables 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 the session variable.
In order for you to see this problem, I created the test code and uploaded it to GitHub. You can find the test script here again. Locally, you need to use the test code with an instance, and then open the browser console to see the request and response time.
As we see in the sample code in this file, if we like the following code, create multiple requests ...

session_start();sleep(5);

Each request from the same user is completed until the previous request completes. It will take 5s, because the session file is not released until the script is completed. Therefore, when Session_Start () is called for the first time, the new request is blocked. That will kill the idea of an asynchronous request, which means that multiple requests are 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 does not have to wait to run because it can call session_start () without any problems.

Summarize

PHP is a bit tricky and can cause you to worry about why strange things happen. But once you understand how things work, everything becomes meaningful, and you can think better about solving problems.

Source: http://www.ido321.com/1577.html

This article according to @eslam Mahmoud's "Fix the AJAX requests that makes PHP takes Too Long to Respond" translation, the entire translation with my own understanding and thought, if the translation is not good or the wrong place also please peer friends pointing. If you want to reprint this translation, please specify 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 the solution of the AJAX request to the PHP response time is too long, including the content, I hope the PHP tutorial interested in a friend helpful.

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