Skip to main content
Inspiring
July 11, 2023
Question

How to change cfinclude to cflocation?

  • July 11, 2023
  • 2 replies
  • 459 views

Hi,

I have a login page Login.cfm for which I have written code in application.cfc's onRequestStart method,

<cffunction name="OnRequestStart">
<cfargument name = "request" required="true"/>

<cfif IsDefined("Form.logout")>
<cflogout>
</cfif>

<cflogin>
<cfif NOT IsDefined("cflogin")>
    <cfinclude template="Login.cfm">
    <cfabort>
<cfelse>
    <cfif cflogin.name IS "" OR cflogin.password IS "">
        <cfoutput>
            <h2>You must enter text in both the User Name
            and Password fields.
            </h2>
        </cfoutput>
        <cfinclude template="Login.cfm">
        <cfabort>
    <cfelse>
        <cfquery name="loginQuery" dataSource="tdweb">
            SELECT username,password,roles
            FROM LoginInfo
            WHERE
            username = '#cflogin.name#'
            AND password = '#cflogin.password#'
        </cfquery>

        <cfif loginQuery.roles NEQ "">
            <cfloginuser name="#cflogin.name#" Password = "#cflogin.password#"
            roles="#loginQuery.roles#">
        <cfelse>
            <cfoutput>
                <H2>Your login information is not valid.<br>
                Please Try again</H2>
            </cfoutput>
            <cfinclude template="Login.cfm">
            <cfabort>
        </cfif>
    </cfif>
</cfif>
</cflogin>

<cfif GetAuthUser() NEQ "">
    <cfoutput>
        <form action="Login.cfm" method="Post">
            <input type="submit" Name="Logout" value="Logout">
        </form>
    </cfoutput>
</cfif>
</cffunction>
 
Now instead of cfinclude I tried using cflocation like this <cflocation url="Login.cfm"  addToken="no">,
 
It throws 127.0.0.1 redirected you too many times error page. Please suggest a solution for this. I don't know the difference between cfinclude and cflocation. Both seems to be same.
 
Also one more help please. After logging in, the user should land in a page called UserList.cfm. That should be first default page that user should see after logging in. Please let me know how to do that as well.  And when user clicks logout button it should take to  to the login.cfm page again. Please let me know how to do that as well.Thank you.
    This topic has been closed for replies.

    2 replies

    Legend
    July 11, 2023

    First, the difference between CFINCLUDE and CFLOCATION:

    • CFINCLUDE will read whatever is in the included file and execute it on the calling page.  Think of anything in the CFINCLUDE script as actually being written in the same code block.
    • CFLOCATION will redirect the user to the page you specified.  I believe, behind the scenes, there's an HTTP 302 redirect happening.

    So, in your application file, when the 'login.cfm' page loads, the onRequest function fires and sees that this line is TRUE: <cfif cflogin.name IS "" OR cflogin.password IS "">

     

    With that being TRUE, a CFLOCATION will redirect the user, according to your code, back to login.cfm.  Now, the same thing will happen.  Over and over which is why you get the too many redirects error.

     

    You need to rethink your logic here.  Perhaps add an exception for the login page?

    Inspiring
    July 11, 2023

    Hi sdsinc_pmascari, thanks for replying. Add exception for login page means cftry or cfcatch ? Is it possible to write any CFIF logic that will not execute the <cflocation url="login.cfm"> 

    Legend
    July 11, 2023

    Once you find out which CFIF is causing your redirect loop, you could add the following:

    AND cgi.script_name NEQ "/yourScriptPath/login.cfm"

    Inspiring
    July 11, 2023

    My Login.cfm file is ,

    <H2>Please Log In</H2>
    <cfoutput>
    <form action="#CGI.script_name#?#CGI.query_string#" method="Post">
    <table>
    <tr>
    <td>user name:</td>
    <td><input type="text" name="j_username"></td>
    </tr>
    <tr>
    <td>password:</td>
    <td><input type="password" name="j_password"></td>
    </tr>
    </table>
    <br>
    <input type="submit" value="Log In">
    </form>
    </cfoutput>