Jump to content


Photo

Ajax and sessions


  • Please log in to reply
10 replies to this topic

#1 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 10 September 2006 - 02:13 PM

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.
<?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']);				
		}
	}
?>

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:

Array
(
    [REMOTE_ADRR] => 127.0.0.1
    [PHP_SESSID] => aca8ovfo7kbbgv1en8levipf10
)

Content of $_SESSION after navigation, content (and var dump) updated by ajax:

Array
(
    [REMOTE_ADRR] => 127.0.0.1
    [PHP_SESSID] => aca8ovfo7kbbgv1en8levipf10
    [applicationState] => usability
)

After using refresh:

Array
(
    [REMOTE_ADRR] => 127.0.0.1
    [PHP_SESSID] => aca8ovfo7kbbgv1en8levipf10
)


I am absolutely positive I don't unset ANY session variables whatsoever ANWHERE.

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 10 September 2006 - 03:44 PM

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?

Info: PHP Manual


#3 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 10 September 2006 - 03:57 PM

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.


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.

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 10 September 2006 - 03:58 PM

But you've proved yourself that it doesn't work.

Info: PHP Manual


#5 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 10 September 2006 - 04:08 PM

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/Pr...2616-sec14.html

#6 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 10 September 2006 - 05:06 PM

I found something that probably has something to do with it: http://www.chipmunkn...e/asyncsessions

Race conditions. Another thing learned.

#7 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 10 September 2006 - 05:21 PM

For the record: session_write_close() is the solution.

#8 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 10 September 2006 - 08:50 PM

Can you clarify what you mean?

Info: PHP Manual


#9 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 10 September 2006 - 11:23 PM

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.


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().

#10 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 15 September 2006 - 10:55 PM

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#11 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 16 September 2006 - 12:08 AM

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.


Yes I 'just store stuff', did you READ this thread?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users