We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Coldfusion - redirect to original requested page after login

Medium Priority
873 Views
Last Modified: 2013-12-24
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?
Comment
Watch Question

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

Author

Commented:
im not sure where i should set the variable?

<cfset session.redirect = cgi.script_name />

in the application.cfm?

Commented:
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.

Author

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>

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

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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>

Commented:
Yep,

That's the way to do it.

Regards
Plucka
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.