Skip to main content
April 25, 2008
Question

Redirect after login

  • April 25, 2008
  • 5 replies
  • 3044 views
I have :
<cfif #SESSION.usrlogin# neq "true">
<a href="login.cfm">login</a>
<cfelse>
Page Content
</cfif>

I'd like the cflocation in the login action page to be generic enough to resuse sitewide so the user, upon logging in from here, is redirected to the page they were trying to view.

Thanks,

Bob
This topic has been closed for replies.

5 replies

BKBK
Community Expert
April 28, 2008
Could <cfinclude template="loginPage.cfm"> be better than <cflocation url="loginPage.cfm"> in the last code?


BKBK
Community Expert
April 27, 2008
Coldandfuzzy wrote:

I have :
<cfif #SESSION.usrlogin# neq "true">
<a href="login.cfm">login</a>
<cfelse>
Page Content
</cfif>


I'd like the cflocation in the login action page to be generic enough to resuse sitewide so the user, upon logging in from here, is redirected to the page they were trying to view.


If you're on MX7.x or CF8.x, then there is at least one solution that fits your three themes of session, login and requested page. It makes use of Application.cfc.

In Application.cfc
===============
<cffunction name="onRequestStart">
<cfargument name = "targetPage" type="String" required=true >
<cfif (NOT isDefined("session.isUsrLogggedIn") or NOT session.isUsrLoggedIn) and (NOT listLast(CF_TEMPLATE_PATH,"\") is "loginPage.cfm")>
<cfset session.requestedpage=arguments.targetpage>
<cflocation url="loginPage.cfm">
</cfif>
</cffunction>


loginPage.cfm
===============
<h2>login page</h2>
To simulate login, press the button. Then Coldfusion will log you in and redirect you to the page you originally requested.

<p>
<cfif isDefined("form.loggedIn")>
<!--- validation code goes in here --->
<!--- The following code is the kind that will run if validation is successful --->
<cfset session.isUsrLoggedIn = true>
<cflocation url="#session.requestedpage#">
</cfif>
<form action="#cgi.script_name#" method="post">
<input name="loggedIn" value="Press to simulate login" type="Submit">
</form>



edited: changed login boolean to session.isUsrLoggedIn


Inspiring
April 25, 2008
coldandfuzzy wrote:
> I have :
> <cfif #SESSION.usrlogin# neq "true">
> <a href="login.cfm">login</a>
> <cfelse>
> Page Content
> </cfif>
>
> I'd like the cflocation in the login action page to be generic enough to
> resuse sitewide so the user, upon logging in from here, is redirected to the
> page they were trying to view.
>
> Thanks,
>
> Bob
>


A easy way to do this is make the content of the desired page based on
the login state. Instead of just displaying a link to the login.cfm,
include it here. Then when the page is refreshed with a successful
login, the actual content is displayed.


<cfif #SESSION.usrlogin# neq "true">
<cfinclude template-"login.cfm">
<cfelse>
Page Content
</cfif>
April 25, 2008
I see what you are saying and I have done that on some areas of the site. Even so, I need to redirect back to the original page after logging in.

Bob
Inspiring
April 25, 2008
You might add the original page url to the querystring of the login.cfm link, then redirect the user there on your login.cfm page with cflocation. I believe the ASP.NET forms login methodology does something similar.

One caveat: check the contents of the querystring and validate that the url is valid before using cflocation tag.
April 25, 2008
What I'd like to do is to make it go back in browser history rather than being page specific. A perfect example is on this board. If you want to post but are not logged in, you log in and then it directs you to the form to post here.

Bob
Inspiring
April 25, 2008
quote:

Originally posted by: coldandfuzzy
What I'd like to do is to make it go back in browser history rather than being page specific. A perfect example is on this board. If you want to post but are not logged in, you log in and then it directs you to the form to post here.

Bob

Look at your cgi variables. See anything useful?
Inspiring
April 25, 2008
what about this

<cfif #SESSION.usrlogin# neq "true">
<cfset session.page_they_wanted = "templatename.cfm" >
<cflocation url="login.cfm" >
<cfelse>
Page Content
</cfif>

then in your login.cfm page, after they've successfully verified themselves and youve set the session.usrlogin variable do something like this..

<p>youve now logged in successfully</p>
<a href="#session.page_they_wanted" >click here to go to the page you asked for earlier </a>

(or you could cflocation them to the page seamlessley if you prefer)

if youre feeling cautious you might also want to record the time of setting the session.page_they_wanted and only offer/cflocate them to the page if they asked for it within (say) the past 5 minutes (other wise it might seem a bit odd to them (or someone else subsequentally using the same pc) if when they login 45 minutes later they get whisked off to some page they tried to access ages ago

HTH

do say if you want any more explanation on what i'm talking about here

Nick