• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Redirect after login

Guest
Apr 25, 2008 Apr 25, 2008

Copy link to clipboard

Copied

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
TOPICS
Advanced techniques

Views

2.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 25, 2008 Apr 25, 2008

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Apr 25, 2008 Apr 25, 2008

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 25, 2008 Apr 25, 2008

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 25, 2008 Apr 25, 2008

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 25, 2008 Apr 25, 2008

Copy link to clipboard

Copied

Do you mean the URL or requested page?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 25, 2008 Apr 25, 2008

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 25, 2008 Apr 25, 2008

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 25, 2008 Apr 25, 2008

Copy link to clipboard

Copied

coldandfuzzy wrote:
> 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


But you are still on the original page, no redirect needed.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 25, 2008 Apr 25, 2008

Copy link to clipboard

Copied

I see what you mean. I was trying to have one login page go to the requested page. I ended up using your suggestion though. It's just that I had to do it in many pages.

Thanks,

Bob

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 25, 2008 Apr 25, 2008

Copy link to clipboard

Copied

coldandfuzzy wrote:
> It's just that I had to do it in many pages.
>
> Thanks,
>
> Bob

That's where pulling this code into a common
Application.cfm|Application.cfc file helps.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 27, 2008 Apr 27, 2008

Copy link to clipboard

Copied

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


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 27, 2008 Apr 27, 2008

Copy link to clipboard

Copied

LATEST
Could <cfinclude template="loginPage.cfm"> be better than <cflocation url="loginPage.cfm"> in the last code?


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation