0

The situation:

I have a site with multiple pages accessible through the menu, after login. On the dashboard page (the page you see right after login), there is a AJAX call getting the data from the PHP-Controller (zend framework). This call does about 20 queries to 2 different databases and a lot of data manipulations. It can take up to 5 seconds to load everything. So far, so good!

My problem:

When I go to another page through the menu, right after seeing the home page, the ajax call gets cancelled, and a new request is send to a new PHP-Controller. But because I'm working with php-sessions (through the zend-framework), I'm not able to do a new request before session_write_close() is called (at the end of the long-taking AJAX request). Is there a possibility that the previous call gets cancelled?

A not decent solution:

Right now, I've called session_write_close() at the beginning of my AJAX request, and this fixes the problem. Afterwards I still read from the session, but I've read, and confirmed that this is not an issue. Nevertheless, calling session_write_close() is not the prettiest thing to do, and I'm working with a few guys on this project, which has a lot of files and code paths, and won't be finished in the next 5 years... So I can't use session_write_close(), since this would not be maintainable. Especially because when you write to the session, PHP would not throw an exception. The data would simply not be saved.

So my question is:

Is there a decent alternative to session_write_close(), that let me interrupt/cancel an AJAX call (PHP-wise), to instantly go to another page, or is there a possibility to change a setting so that PHP would throw an exception when trying to write after session_write_close() is called?

Thanks In advance!

Thomas Stubbe
  • 1,945
  • 5
  • 26
  • 40

1 Answers1

0

One option would be to store the session values in a database. So once you have the session_id you can read and write to the DB with updated values. It would look something like this:

$session_id = session_id();
session_write_close();
$query = $dbh->prepare("select * from tbl_sessions where session_id=?");
$result = $query->execute(array($session_id));
$values = $result->fetch();
$session_values = json_decode($values->data); //data would be a text column with a json or serialized array
//you could update any values then update tbl_sessions if you needed

Or you can override the default session functions like in this answer: set session in database in php

Or as in another answer you can close and open the session as needed: https://stackoverflow.com/a/10046611/1401720

Community
  • 1
  • 1
Pitchinnate
  • 7,517
  • 1
  • 20
  • 37
  • We can't save the sessions to our DB and are forced to use php-sessions. Using a SessionManager would solve the problem, but does still not prevent a user from directly writing to the session after it is closed and would have a lot of impact on the project (every read/write to the session would have to change and pass the SessionManager). The problem is not that the solution is not working, but that it does not force future development to anticipate on these changes. They could still ignore the the SessionManager.. – Thomas Stubbe Apr 25 '14 at 14:21
  • Yes they could ignore it but that is why you should have coding standards and make sure all developers working on the project know them. – Pitchinnate Apr 25 '14 at 14:24
  • The best solution would be to cancel the PHP-script execution of the AJAX call when the client/browser cancels the request. But I have not yet heard of such a concept: every request with a session gets queued in PHP until "session_write_close()" is called. – Thomas Stubbe Apr 25 '14 at 14:24
  • We do have coding standards, but we are with about 10 developers, in a young team (company is younger than 3 years), using Agile... I would rather not implement this and force clients to wait up to 5 seconds. – Thomas Stubbe Apr 25 '14 at 14:25
  • Is it only the dashboard that is slow and takes a while to load everything? – Pitchinnate Apr 25 '14 at 14:26
  • The dashboard and one other page involving open layer maps, which is even slower. But the dashboard is the most important one, since every client must pass this page to view another one. – Thomas Stubbe Apr 25 '14 at 14:29
  • Only other possible solution I can think of is on the page have a loading indicator and disable menus, links, etc... till after load is complete. – Pitchinnate Apr 25 '14 at 14:59
  • We already have loading spinners, but the links are not disabled... But that matters not too much, because after the content of the dashboard is loaded, the new page loads right away – Thomas Stubbe Apr 25 '14 at 15:09