PHP Classes

Fix the AJAX Requests that Make PHP Take Too Long to Respond

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog Fix the AJAX Requests...   Post a comment Post a comment   See comments See comments (26)   Trackbacks (0)  

Author:

Viewers: 1,102

Last month viewers: 101

Categories: PHP Tutorials

Nowadays we develop applications that depend a lot on AJAX requests, in some cases close to 100% of the pages. Sometimes we notice that when a Web page sends two or more AJAX requests, PHP takes too much time to respond and then the responses are returned almost at the same time.

Chances are that the problem may be caused by the way you handle PHP sessions. Read this article to understand this problem and what you can do to avoid it.




Loaded Article

Contents

What is a PHP Session?

What is AJAX?

The Problem

The Cause

The Solution

Conclusion


PHP and AJAXWhat is a PHP Session?

To understand the problem it is necessary to understand what are PHP Sessions, AJAX and how they interfere.

Imagine you are developing a Web application and want to recognize the user. You want remember who he is in all the pages without making him login every time. In this case you may use cookies or sessions.

As you may have realized by now, sessions are a way of storing users' information in a way that can retrieve the user information from any page. Unlike cookies, session information is stored on the server, so the users have no way of changing it directly.

By default, sessions last until the user closes the browser or after the user remains inactive for a timeout period defined in the PHP configuration.

Whenever you want to store or retrieve user data in PHP page, you must start the page with calling session_start() function,  so you will have access to session data using the $_SESSION variable.

What is AJAX?

AJAX stands for Asynchronous JavaScript and XML. It is a way of sending and receiving data from a server without reloading the whole page.

We use this method to send data and retrieve information from the server in faster way, as we do not want to receive the whole page and render it in the browser because that would be much slower.

As result we can update part of the Web page the user is viewing, like what users see when they scroll down in Facebook time line page, as new content gets added without reloading the page.

The Problem

Developing Web applications based almost 100% on AJAX is not a new thing. But when a Web page sends two or more AJAX requests almost at the same time, sometimes you notice that they take too much time and then the requests finish almost at the same time.

The Cause

When you send an AJAX request to a server and your PHP script starts with session_start(),  that call will lock the PHP session file.

As you may know, by default PHP stores session data in files on server. Since no more than one PHP request can change the same session file, two simultaneous PHP requests may cause a classic file lock condition. So any other request handled by PHP that calls session_start() for the same user will have to wait until the first request finishes.

Nowadays, most, if not all, PHP frameworks use session_start() as first thing in the main file. So, if you are using frameworks or other libraries that call session_start(), you may be causing session file lock, thus delaying simultaneous AJAX requests sent from pages for the same user using the same browser window.

The Solution

Calling the session_write_close() function will make PHP write and close the session file on the server, thus releasing the session file, so another request can access it.

The current script will continue working normally after calling this function but you should know that you are not allowed to change any session variable after using session_write_close(); in the same script. Other simultaneous requests handled by PHP can lock the session file and change session variables.

I have created test code to let you see the problem and uploaded it to GitHub. You can find it here. You will need to use it for instance under your local host. Open the browser console to see the request and response times.

As we see in the code in this file for example, if we made more than one request to a page that has code like this...

session_start();
sleep(5);

... each request done by the same user will wait for the previous one to finish. It will take 5 seconds because the session file have not be released until the script finishes. So a new request will be blocked on the first call to session_start().  That kills the idea of asynchronous requests, i.e. more than one request be sent and executed at the same time.

If we changed the code to be like in that file...

session_start();
// do something useful here
session_write_close();
sleep(5);

... the third line in the code will release the session file lock, so another parallel request can run without waiting, as it can call session_start() without any problems.

Conclusion

PHP has these subtleties that may leave you wondering why odd things may happen. But once you understand how things work under the hood, all starts making sense and you can think better how to solve problems like this of session file locking.

Post a comment if you liked this article or have other questions.




You need to be a registered user or login to post a comment

1,616,107 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

13. Ajax Request - Rocky Osborne (2015-05-22 01:15)
"fix the Ajax requests..."... - 1 reply
Read the whole comment and replies

12. Thanks a million. - Guillermo (2015-05-09 02:58)
thank you my friend.... - 1 reply
Read the whole comment and replies

11. session vs singleton - Yoni Lamri (2015-05-05 08:54)
session vs singleton... - 1 reply
Read the whole comment and replies

1. session - Chris backhouse (2015-05-04 23:17)
database... - 1 reply
Read the whole comment and replies

2. Store session in database - Nikola Radovanovic (2015-05-04 23:17)
For me better solution is to store session in database... - 1 reply
Read the whole comment and replies

3. If session is store in database - chetan gadgilwar (2015-05-04 23:17)
store session in database... - 1 reply
Read the whole comment and replies

4. Session_Write_Close() - Wayne Franklin (2015-05-04 23:17)
Helpful idea... - 1 reply
Read the whole comment and replies

5. Fix the AJAX Requests that Make PHP Take Too Long to Respond - noname (2015-05-04 23:17)
No file based session storage... - 1 reply
Read the whole comment and replies

6. Interesting information - Eduardo Weidman Barijan (2015-05-04 23:16)
It is the first time I heard about file locking in PHP... - 1 reply
Read the whole comment and replies

7. Warning messages from Second Session Start - Wayne Franklin (2015-05-04 23:16)
To eliminate warning messages... - 1 reply
Read the whole comment and replies



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog Fix the AJAX Requests...   Post a comment Post a comment   See comments See comments (26)   Trackbacks (0)