Shaun Mccran

My digital playground

20
M
A
Y
2010

Building Intelligent sessions into your framework

Have you ever been logged into an application and had your session timeout, then when you log back in you are returned to a different place altogether?

This article deals with a way to mark where a user was in your application and return them to that location when they log back in. It also allows a user to deep link into an application. IE when they have a bookmarked link instead of being passed to the home page, they are passed through to their original destination.

This relies quite a lot on knowing the processing order of your application well. IE what order your templates are run in and more specifically what order any functions are run in. You could do this effectively in your 'onRequestStart' function, but in this example I am using a fuseBox framework.

In my controller CFC's I have a preFuseAction. This function runs before any of the other controller functions, making it an ideal place to put any granular security or user handling functions.

view plain print about
1<cffunction name="prefuseaction">
2    <cfargument name="myFusebox" />
3    <cfargument name="event" />
4
5<cfset cookie.requestedTemplate = 'index.cfm?' & cgi.query_string>
6
7<!--- check that user is logged in --->
8<cfif NOT isdefined('session.loggedIn')>
9<!--- kick the user to somewhere else --->
10</cfif>

In the code above before I do anything I store the requested URL as a cookie value. This happens before I check for security. In this way it is stored whether a user is delivered to the requested template, or to the login screen. Then I run a check on a session value to see if I should continue onwards.

In this way we always have a cookie value of the last page a user requested. Whether hey got there or not.

Next we need to use the cookie value in our login script.

view plain print about
1<cfif variables.login>
2
3<cfif isDefined('cookie.requestedTemplate') and len(cookie.requestedTemplate)>
4
5<cflocation url="#cookie.requestedTemplate#" addtoken="false">
6</cfif>
7
8<cflocation url="#myself#" addtoken="false">
9</cfif>

In the code here we are checking that we have successfully logged in, and if we have we check if the cookie value exists, and if there is a value there.

If there is a value we pass the user to the last location they requested prior to login, otherwise we just pass them to the logged in home page.

This works for deep linking as well because the preFuseAction function will run when a user uses a bookmark, so the bookmark location will be stored as a cookie value.

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
John Whish's Gravatar It's worth checking to see if a form was being submitted before you set cookie.requestedTemplate otherwise you'll get errors when you redirect them.
# Posted By John Whish | 20/05/2010 12:25
Shaun McCran's Gravatar Ah good point, I guess if a user is pushed back to a form 'success' template then it will probably flip out.

I suppose you could wrap it in a check for a 'POST' variable and escape it if one exists.

Thanks John!
# Posted By Shaun McCran | 20/05/2010 12:31
Julian Halliwell's Gravatar http://userlove.riaforge.org/

Handles GET and POST.
# Posted By Julian Halliwell | 22/05/2010 07:33
Shaun McCran's Gravatar Thanks Julian, I rellay need to check RIAForge before I write some of this stuff.

Shaun
# Posted By Shaun McCran | 23/05/2010 18:09
Simon Silvestor Clarke's Gravatar Building intelligent seasions for the children's is always good because in this way the children's can become http://www.essayzwriting.com/ intelligent. So we should take these things for our children's.
# Posted By Simon Silvestor Clarke | 18/11/2015 03:56
Back to top