• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to change cfinclude to cflocation?

Explorer ,
Jul 11, 2023 Jul 11, 2023

Copy link to clipboard

Copied

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.

Views

241

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 11, 2023 Jul 11, 2023

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 11, 2023 Jul 11, 2023

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 11, 2023 Jul 11, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 11, 2023 Jul 11, 2023

Copy link to clipboard

Copied

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

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 11, 2023 Jul 11, 2023

Copy link to clipboard

Copied

LATEST

I added the statement AND cgi.script_name NEQ "/yourScriptPath/login.cfm" in the first cfif  ,

<cflogin>
    <cfif NOT IsDefined("cflogin") AND cgi.script_name NEQ "/Registration_Web/Login.cfm">
 
when I run it shows coldfusion error , "Element name is undefined in cflogin.", If I put AND cgi.script_name statement in the second cfif it again throws "Too many redirects" error. Could you please let me know if  there is  any other way we can solve this.. or  if there is any other way we can do user authentication. Instead of writing <cflogin> code logic in onRequestStart method of application.cfc can i use any different approach?Without having any  authentication related  code in application.cfc or onRequestStart method, can I do user authentication? Please comment on this, thanks 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation