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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
grizumAuthor Commented:
im not sure where i should set the variable?

<cfset session.redirect = cgi.script_name />

in the application.cfm?
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.
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

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>
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
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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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>
PluckaCommented:
Yep,

That's the way to do it.

Regards
Plucka
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.