This topic is locked

PHP and AJAX

5/5/2015 5:35:17 AM
PHPRunner General questions
S
swanside author

Sorry guys, I found this yesterday and thought I posted it here, makes an intresting read, not sure if its applicable to PHPRunner, or should we add this to any of the files that get generated, or is there any truth in it?
What 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.