Author: Eslam Mahmoud
Last month viewers: 101
Categories: PHP Tutorials
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.
What is a PHP Session?
What is AJAX?
What is a PHP Session?To understand the problem it is necessary to understand what are PHP Sessions, AJAX and how they interfere.
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?
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.
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.
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.
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...
... 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.
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,587,937 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
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