Coldfusion - redirect to original requested page after login

I have an application.cfm that restricts access to a web app if you are not logged in.  If someone is sent a link to some content in the system, it will redirect them to login before viewing the page .. but after they login, it redirects them to the index page instead of the original request url.  

is it possible to set the requested url in a variable somehow .. so i can pass it back to the redirect after login?   would i set the var in application.cfm?
grizumAsked:
Who is Participating?
 
PluckaCommented:
Plucka,

OR BETTER STILL

<cfif session.allow neq "true" and Not find("login.cfm", CGI.SCRIPT_NAME)>
    <cfset session.redirect = CGI.SCRIPT_NAME />
    <cflocation url="login.cfm">
</cfif>

Regards
Plucka
0
 
PluckaCommented:
grizum,

Just put it in a session variable before taking them back to login.

<cfset session.redirect = cgi.script_name />

Then after successsful login

<cfif isDefined("session.redirect")>
    <cflocation url="#session.redirect#" />
</cfif>

Regards
Plucka
0
 
grizumAuthor Commented:
im not sure where i should set the variable?

<cfset session.redirect = cgi.script_name />

in the application.cfm?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
PluckaCommented:
When they return to a book mark, you must already have code that says, hey your not logged in, take to login page.

You need the set in there.

This could be application.cfm, if this is where you send them to login.

If you post your code of application.cfm I could help, you need to be careful about continious loops.
0
 
grizumAuthor Commented:
yes .. the application.cfm handles that .. here is the code for it.

<cfapplication name="ADMIN" clientmanagement="Yes"
                    sessionmanagement="Yes"
                    sessiontimeout="#CreateTimeSpan(0,4,0,0)#"
                    applicationtimeout="#CreateTimeSpan(1,0,0,0)#">
<cfscript>
    if (NOT structKeyExists(session, 'clickstream')) {
        session.clickstream = arrayNew(1);
    }
</cfscript>

<CFPARAM NAME="session.allow" DEFAULT="false">
<CFPARAM NAME="session.userid" DEFAULT="0">

<cfif session.allow neq "true">
      <cfif ListLast(CGI.SCRIPT_NAME, "/") EQ "login.cfm">
      <cfelseif ListLast(CGI.SCRIPT_NAME, "/") EQ "process_login.cfm">
      <cfelse>
<cflocation url="login.cfm">
      </cfif>
</cfif>
0
 
PluckaCommented:
grizum,

<cfif session.allow neq "true">
    <cfif ListLast(CGI.SCRIPT_NAME, "/") EQ "login.cfm">

    <cfelseif ListLast(CGI.SCRIPT_NAME, "/") EQ "process_login.cfm">

    <cfelse>
        <cfset session.redirect = CGI.SCRIPT_NAME />
        <cflocation url="login.cfm">
    </cfif>
</cfif>

Regards
Plucka
0
 
PluckaCommented:
Plucka,

Can I pick on your code :) All this, could be done a lot more easily.

<cfif session.allow neq "true">
    <cfif ListLast(CGI.SCRIPT_NAME, "/") EQ "login.cfm">

    <cfelseif ListLast(CGI.SCRIPT_NAME, "/") EQ "process_login.cfm">

    <cfelse>
        <cfset session.redirect = CGI.SCRIPT_NAME />
        <cflocation url="login.cfm">
    </cfif>
</cfif>


LIKE THIS

<cfif session.allow neq "true">
    <cfif Not ListLast(CGI.SCRIPT_NAME, "/") contains "login.cfm">
        <cfset session.redirect = CGI.SCRIPT_NAME />
        <cflocation url="login.cfm">
    </cfif>
</cfif>


OR EVEN THIS

<cfif session.allow neq "true">
    <cfif Not find("login.cfm", CGI.SCRIPT_NAME)>
        <cfset session.redirect = CGI.SCRIPT_NAME />
        <cflocation url="login.cfm">
    </cfif>
</cfif>

Regards
Plucka
0
 
grizumAuthor Commented:
awesome man ... thanks!  

i added one thing, thought i would post it here for others and to verify i did it the right way ... i needed it to include the query_string

<cfif session.allow neq "true" and Not find("login.cfm", CGI.SCRIPT_NAME)>
    <cfset session.redirect = CGI.SCRIPT_NAME & "?" & CGI.Query_String />
    <cflocation url="login.cfm">
</cfif>
0
 
PluckaCommented:
Yep,

That's the way to do it.

Regards
Plucka
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.