12 Replies Latest reply on Apr 27, 2008 9:33 PM by BKBK

    Redirect after login

    coldandfuzzy
      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
        • 1. Re: Redirect after login
          happysailingdude
          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
          • 2. Redirect after login
            JR "Bob" Dobbs-qSBHQ2 Level 3
            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.
            • 3. Re: Redirect after login
              coldandfuzzy Level 1
              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
              • 4. Re: Redirect after login
                Level 7
                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>
                • 5. Re: Redirect after login
                  Dan Bracuk Level 5
                  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?
                  • 6. Re: Redirect after login
                    coldandfuzzy Level 1
                    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
                    • 7. Re: Redirect after login
                      coldandfuzzy Level 1
                      Do you mean the URL or requested page?
                      • 8. Re: Redirect after login
                        Level 7
                        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.
                        • 9. Re: Redirect after login
                          coldandfuzzy Level 1
                          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
                          • 10. Re: Redirect after login
                            Level 7
                            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.
                            • 11. Redirect after login
                              BKBK Adobe Community Professional & MVP
                              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


                              • 12. Re: Redirect after login
                                BKBK Adobe Community Professional & MVP
                                Could <cfinclude template="loginPage.cfm"> be better than <cflocation url="loginPage.cfm"> in the last code?