Question

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!

Était-ce utile?

La solution

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top