Jump to content

Ajax and sessions


448191

Recommended Posts

I've been trying to get my ajax application to maintain it's state upon page refresh.

The only way I could think of was using sessions. When the browser requests an update by XmlHttpRequest, I store a session variable describing the created state. Upon traditional page genaration (like with a refresh), I first check if that variable is set, and recreate the state if it is.

Problem is the variable is not set anymore when refreshing! I checked if it was when requesting an update, and it is.
[code]
<?php
public function finishProcess(){
if(isset($_GET['update'])){
self::processUpdate();
global $response;
$response->addTitle(Navigation::getTitle());
dump_arr($GLOBALS);
}
else {
if(isset($_SESSION['applicationState'])){ // && $GLOBALS['client']->ajax
echo $_SESSION['applicationState'];
$GLOBALS['backbone']->uri = $_SERVER['REQUEST_URI'].'?state='.$_SESSION['applicationState'];
}
self::processUri($GLOBALS['backbone']->uri);
Navigation::setTitle($GLOBALS['backbone']);
}
}
?>[/code]

I even checked if, by some fluke, two different sessions id's were present (although I do have protection against session hijacking wich should trigger on that), but the're the same.

Content of $_SESSION initial load:
[quote]Array
(
    [REMOTE_ADRR] => 127.0.0.1
    [PHP_SESSID] => aca8ovfo7kbbgv1en8levipf10
)[/quote]
Content of $_SESSION after navigation, content (and var dump) updated by ajax:
[quote]Array
(
    [REMOTE_ADRR] => 127.0.0.1
    [PHP_SESSID] => aca8ovfo7kbbgv1en8levipf10
    [applicationState] => usability
)[/quote]
After using refresh:
[quote]Array
(
    [REMOTE_ADRR] => 127.0.0.1
    [PHP_SESSID] => aca8ovfo7kbbgv1en8levipf10
)
[/quote]

I am absolutely positive I don't unset ANY session variables whatsoever ANWHERE.
Link to comment
Share on other sites

I'm not 100% sure on this... but I don't think you can update a session using AJAX and have that stick after a page refresh.  The whole point of using AJAX is to refresh the page without moving away from it or refreshing the page.  You may consider writing to a file in the background on the server and storing your information there.  Otherwise, I don't really know of a way around this.

You could tie the file name to the session id and just keep a script in one of your pages that deletes any files older than maybe 24 hours.  You could also delete these files when the person leaves the page or logs out.  Not sure what your setup is.

Does that help?
Link to comment
Share on other sites

[quote author=ober link=topic=107541.msg431654#msg431654 date=1157903066]
I'm not 100% sure on this... but I don't think you can update a session using AJAX and have that stick after a page refresh.  The whole point of using AJAX is to refresh the page without moving away from it or refreshing the page. [/quote]

I don't believe you.  :P Sessions are just a bunch of variables on the server grouped by an id, right? If I add a variable to session with id 'a', and later I request the same var within a session that (evidently) has the same id, it should be available! We can't have session variables "dissapear"!

Something isn't right.
Link to comment
Share on other sites

True, it doesn't work right now. The question is WHY doesn't it work!

I've been reviewing all relevant code paths, and can say I honestly don't have a clue.

I am not giving up, this SHOULD work. If it doesn't I need to know why!

Edit: An ajax request is just like any other http request to the server, so why should it dump vars created by that request? A server doesn't know the difference between a refresh and a regular request does it?

I mean, are there http headers that indicate the request is a refresh? I'm looking that up.

Edit: can't find anything that indicates so.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
Link to comment
Share on other sites

[quote]Session data is usually stored after your script terminated without the need to call session_write_close(), but as session data is locked to prevent concurrent writes only one script may operate on a session at any time. When using framesets together with sessions you will experience the frames loading one by one due to this locking. You can reduce the time needed to load all the frames by ending the session as soon as all changes to session variables are done.[/quote]

I won't pretend I fully understand how that would cause session variables to be lost upon page refresh, but I think it has something to do with php not having terminated the execution enviroment for the previous request, and thus somehow the whole session is overwriten (See also the link I posted on 'race conditions').

Calling session_write_close() (while end_session() might also work), makes it possible for a new execution enviroment to restart the session properly.

I think.  ;D

Edit: I realized I was already using end_session(), so it appears one definitaly needs session_write_close().
Link to comment
Share on other sites

I really don't feel that using sessions makes ajax, ajax does it.  Ajax, asynchronous javascript and xml, or text.  So using a text file, or .php file with information on it, but storing it in a session, what do you mean.  UNless you just store stuff in a session, like with php, like you normally do, but what do you mean with this.
Link to comment
Share on other sites

[quote author=businessman332211 link=topic=107541.msg435229#msg435229 date=1158360944]
I really don't feel that using sessions makes ajax, ajax does it.  Ajax, asynchronous javascript and xml, or text.  So using a text file, or .php file with information on it, but storing it in a session, what do you mean.  UNless you just store stuff in a session, like with php, like you normally do, but what do you mean with this.
[/quote]

Yes I 'just store stuff', did you READ this thread?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.