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

CFLogin Not Behaving as Expected

Guest
Dec 07, 2009 Dec 07, 2009

Hello,

I'm creating a login system based on one in the live articles (found here: http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7c30.html... ) Basically, am trying to modify the files so that the Application.cfc sets variables and returns them to the loginform (if login fails) or redirects to a secured page if login succeeds.

Here's my modified code: (I've added comments here in blue to explain my logic)

Application.cfc

<cfcomponent>
<cfset This.name = "LoginAdminTool">
<cfset This.Sessionmanagement="True">
<cfset This.loginstorage="session">

<cffunction name="OnRequestStart">
    <cfargument name = "request" required="true"/>
    <cfif IsDefined("Form.logout")>
        <cflogout>
    </cfif>

    <cflogin>
        <cfif NOT IsDefined("cflogin")>
            <cfinclude template="loginform.cfm">
            <cfabort>
        <cfelse>
            <cfset Session.loginErrEmptyString = "False"> This resets values of variables in the login form
            <cfset Session.loginErrInvalidString = "False"> This resets values of variables in the login form
            <cfif cflogin.name IS "" OR cflogin.password IS ""> 
                <cfoutput>
                    <cfset Session.loginErrEmptyString = "True"> If the string is empty, set the variable to true, return to login
                    <cflocation url="loginform.cfm">
                </cfoutput>
                <cfabort>
            <cfelse>
                <cfquery name="loginQuery" dataSource="myds"> If there is a  value, validate it against my db...
                SELECT userName, userPass, userRole
                FROM security
                WHERE
                    userName = '#cflogin.name#'
                    AND userPass = '#cflogin.password#'
                </cfquery>
                <cfif loginQuery.userRole NEQ "">
                    <cfloginuser name="#cflogin.name#" Password = "#cflogin.password#" roles="#loginQuery.userRole#">
                    <cfset Session.userLoggedIn = "True"> if a result comes back, setuser logged in to true and redirect him to index.cfm
                    <cflocation url="index.cfm">
                <cfelse>
                    <cfoutput>
                        <cfset Session.loginErrInvalidString = "True"> Otherwise login is invalid... return to form
                        <cflocation url="loginform.cfm">
                    </cfoutput>    
                    <cfabort>
                </cfif>
            </cfif>    
        </cfif>
    </cflogin>
 
</cffunction>
</cfcomponent>

And here is the form:

                <cfoutput>
                <form action="#CGI.script_name#?#CGI.query_string#" method="Post">
                <fieldset>
                <ul class="logIn">
                    <li><label for="userName">Username</label></li>
                    <li><input type="text" name="j_username" id="userName"></li>
                    <li><label for="userPass">Password</label></li>
                    <li><input type="password" name="j_password" id="userPass"></li>
                    <li><input type="submit" value="Log In"></li>
                </ul>
                </fieldset>             
                </form>
                </cfoutput>
            </div><!--/box_sml-->
            <div class="box">
                <cfif Session.LoginErrEmptyString IS "True"> If the strings were empty error comes back, the page displays this error code
                    <cfoutput>
                    <h2>You must enter text in both the Username and Password fields.</h2>
                    </cfoutput>
                <cfelse>                   
                    <cfif Session.LoginErrInvalidString IS "True"> If login is invalid, it does that
                        <cfoutput>
                        <h2>The login information entered is invalid. Please Try again.</h2>
                        </cfoutput>
                    </cfif>
                </cfif>
                <cfdump var="#cgi#">
                <cfdump var="#session#">

The problem i am having is that, even when I am logging in correctly, the Application.cfc is not redirecting to index.cfm.... does anyone know the cause of this issue? Any help would be great appreciated... I'm sure there are many ways to code my logic better, but my intent was to avoid Application.cfc dumping html tags above the include="loginform.cfm" tag as per original script. I want to create my error messages within the form page itself..

360
Translate
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
Community Expert ,
Dec 09, 2009 Dec 09, 2009
LATEST

Test by replacing <cfif loginQuery.userRole NEQ ""> with <cfif loginQuery.recordcount GT 0>

Translate
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