Weird Thoughts From Eric's Head

Tags - Categories : All | AJAX | BUSINESS | PERSONAL | PROGRAMMING | BOOK REVIEW

Updating User Session with Ajax - Round 2

Well back by popular demand is updating your user's session with Ajax. Back in this blog posting: http://radio.javaranch.com/pascarello/2005/07/05/1120592884938.html I wrote a little script that used a confirm to update the session with Ajax.

Now of course that causes some trouble with the user not answering the confirm, removing focus from the element, and so on. With that in mind I rewrote the JavaScript functionality to make it more user friendly and give it that rich user interface people want.

It now uses a layer to float onto the page so we do not disrupt the user's actions when it comes on the screen. It also changes the message when the message has been on the screen after a set period of time. No more update your session message after it has expired. Plus I am using some code from my book Ajax In Action to make the request as easy as possible. The .net loader is explained in it in detail, so if you like it then you may want to pick up the book to learn more. The Ajax Session Management script is avaiable on my website at: http://www.pascarello.com/AjaxSessionTimer.aspx

Tell me what you think, any changes, etc.

Eric Pascarello
Moderator of HTML/JavaScript at www.JavaRanch.com
Author of: JavaScript: Your Visual Blueprint for Dynamic Web Pages


Eric.. Thanks so much, very informative article and answers.. I just have a quick question, in order the scripts and the files that u provided on your website to work, do we need any ajax dlls installed along with the project, or those javascript files are enough to get the job done... Anything dlls or ajax components to be added to the visual studio projects, in order for those javascript files to function.. Thanks so much...
You do not have to have any dlls with my examples. The .net loader file is all you need to make the requests.
Everything in the zip file is what you need to run it.
Eric
Hi, I have a question like what happens when we send Ajax call after session expires? Thanks, Ashok
It brings back the login page that your applicaiton would normal bring up. Easy thing to test by changing the timeout on your seesion to a really small number. Eric
Eric, Great article. I implemented it but when I click Update button to refresh the session, I get the " Error fetching data message box...." and the actual window still displays "Updating session". Can you let me know what might be wrong.. Thanks in advance. Rajesh
I'm new to AJAX and .Net. Is there a way to have the script set the timeout time based on the value in the web.config file for session timeout so it doesn't need to be set in 2 places?
You would have to read the config file, to busy to figure out how to look at the Session Part exactly, but reading app. setting is like:

string timeValue = ValueSystem.Configuration.ConfigurationSettings.AppSettings[SOMEKEY].toString();

You would have to write this to a variable, register the variable to the page, and set the object with this string.

Eric

Eric, thank you very match for an excellent article and example. I got the same idea a while ago but never had time to work on it. Recently I started to search for existing solutions, posted my question on a SitePoint forum and then found your article using Google.

Here's a question: I think it would be more secure to automatically redirect browser to a "expired" (back to a login page, or whatever) screen in case user did not respond to to a timeout notification. You answered on Ashok's question above saying that if user clicks on Confirm button too late it brings back the login page that your application would normal bring up. But my point is slightly different: how to automatically bring back the login page in case a user did not click on any button at all?

It would be more secure: suppose I started to fill out some form with my sensitive data and then was called to a different room to fix my supervisor's machine. Meanwhile the form with my SSN stays visible, although a timeout notification is displayed above. It's too bad. It should display login page instead.

Do a document.location.href = document.location.href; when you detect the log in page in the response. This will refresh the page and cause the log in page to show up. Eric

Wow! I didn't think it's so easy. Thank you Eric! Another concern to me is that our site uses frames heavily. Hopefully it would be possible to have only one "central" function working with a timer and Ajax and use onLoad events from other frames to reset a timer... I have to figure it out.

I'm using XMLHttpRequest for several years but only in synchronous mode. It's time to switch to Ajax now. I bought your Ajax book and going read it :)

P.S. Math Quiz is a great idea. It's easier than usual generation of images with screwed words.
Yeah the Math quiz was great up until a few days ago when someone cracked it. Looks like it has to change once again. For your question about the frames, you would have to keep the timer and code in one central location and add some functionality to reset the timer on the page load. Something like this might do the trick. parent.FrameName.ajaxTimer.confirmTimer(); Eric
Eric, I'm having the same problem as observed by Rajesh as below. Do you have any advice on th one? Thx. Al ==== Eric, Great article. I implemented it but when I click Update button to refresh the session, I get the " Error fetching data message box...." and the actual window still displays "Updating session". Can you let me know what might be wrong.. Thanks in advance. Rajesh
You specify the correct serverside page? When you call the serverside page manually does it work? Download Firefox and install firebug and enable the XMLHttpRequest in the options so you can see what is happening. Eric
Thanks, Eric, it worked. Another question- instead of the below message "Your session has most likely been expired. Press the Confirm button to verify this." if I want to implement what you suggested below how I may achieve that. Thx. Al "Do a document.location.href = document.location.href; when you detect the log in page in the response. This will refresh the page and cause the log in page to show up. Eric"
replace the function that sets the innerHTML
function extendedWait(){
		  document.getElementById("divAjaxContent").innerHTML = ref.extendedMessage;
		}
with that code. Eric
Eric, Love it. It works like a magic. Everything is great here. I have read and own your book- Ajax in Action- one of the best in my lib. The other is AjaxPatterns. Thx. Keep up the random acts of publishing and helping. Al
Is there any hope for a PHP version?


Add a comment

Title
Body
HTML : b, i, blockquote, br, p, pre, a href="", ul, ol, li
Math Quiz 1 + 4 = (Helps stop blog spam)
Name
E-mail address
Website
Remember me Yes  No 

E-mail addresses are not publicly displayed, so please only leave your e-mail address if you would like to be notified when new comments are added to this blog entry (you can opt-out later).

TrackBack to http://radio.javaranch.com/pascarello/addTrackBack.action?entry=1130246930301