Creating a User Login Area Using a CFC

The first thing we will do is create a table in your database called "Users" with the following fields:

Field Name

Type

userID

AutoNumber

username

text

password

text



We will be creating 3 pages:
▪ loginCFC.cfc
▪ login.cfm
▪ logout.cfm

The first page that we will create is "loginCFC.cfc". 
<cfcomponent name="LoginCFC">
    <cfscript>

        // call our internal initialize function
        init();
    </cfscript>

    <cffunction name=
"init" access="public">
        <!--- initialize data for component --->
        <cfparam name="request.dsn" default="loginCFC">
    </cffunction>

    <cffunction access=
"public" name="verifyUser">
        <!--- make sure username and password are required --->
        <cfargument name="getUsername" type="string" required="yes">
        <cfargument name=
"getPassword" type="string" required="yes">

        <!--- query the database for the username and password that were passed --->
        <cfquery name="verifyusername" datasource="#request.dsn#">
            SELECT username
            FROM Users
            WHERE username = '#arguments.getUsername#' 
            AND password = '#arguments.getPassword#'
        </cfquery>

        <!--- return the result --->
        <cfif verifyusername.recordCount>
            <cfreturn verifyusername.username>
        <cfelse>
            <cfreturn False>
        </cfif>
    </cffunction>
</cfcomponent>

Next we will create the ?login.cfm? page
<!--- Make sure form was submitted --->
<cfif structKeyExists(form,"verify")>
    <cfinvoke component=
"loginCFC" 
                   method=
"verifyUser"
                   returnVariable="verify"
                   getUsername="#form.username#"
                   getPassword=
"#form.password#">
</cfif>

<!--- Login Form --->
<cfform name="security" method="post" action="">
    Username: <cfinput name="username" type="Text"><br>
    Password:<cfinput name="password" type="password"><br>
    <cfinput type="Submit" name="verify">
</cfform>

<!--- If login was successful tell user that the username and password were verified successfully. If not, tell the user their username and/or password was wrong --->
<cfif isDefined("variables.verify")>
    <cfif variables.verify NEQ 0>
       
Verified Successfully!
    <cfelse>
       
Incorrect Username and/or Password!
    </cfif>
</cfif>

Finally we will create the ?logout.cfm? page
<cfscript>
    StructClear(variables);
</cfscript>

<cflocation url=
"login.cfm" addtoken="No">

All ColdFusion Tutorials By Author: Chris
  • Customer Complaint System
    this is just a customer complaint tracking system i put together with coldfusion. i got alot of help from easycfm.com so i figured i would get back with an app. its using an access database to store usernames and passwords. did all of the login authentication code myself because my damn server behaviors were not taking. copy the dbase somewhere and use the HCCTS datasource name enjoy
    Author: chris mcintosh
    Views: 19,400
    Posted Date: Sunday, March 23, 2003
  • Creating a Chat System in ColdFusion without using a database!
    Have you ever wanted to have your own chat room? This tutorial will help you have one, but the best thing about this tutorial is that you will not need to use any type of database!
    Author: Chris A.
    Views: 32,768
    Posted Date: Saturday, May 10, 2003
  • Random Image
    Here is a random image display. You can even upload it to the server. Uses a database.
    Author: Chris
    Views: 19,754
    Posted Date: Friday, July 25, 2003
  • Custom Tag - Card Expiration Date
    This tutorial lets you save time when you need dates for credit card processing.
    Author: Chris
    Views: 9,642
    Posted Date: Wednesday, September 8, 2004
  • Random Banner
    This will show you how to create a banner rotation app. It will track the # of views and clicks. It can display flash or image files.
    Author: Chris
    Views: 13,233
    Posted Date: Friday, December 31, 2004
  • Intro to Amazon web services
    Learn the basics of building your own eCommerce site using Amazon.coms web services and Cold Fusion.
    Author: Chris Gomez
    Views: 16,263
    Posted Date: Saturday, December 3, 2005
  • Creating a User Login Area Using a CFC
    This tutorial will show you how to create a user login area using a CFC. This is just a basic idea which can easily be modified to fit your needs
    Author: Chris
    Views: 16,813
    Posted Date: Sunday, January 8, 2006
  • Quick and Dirty Contact Forms.
    This is just a simple way to dynamicly generate contact forms without the need for a database.
    Author: Chris Bunting
    Views: 14,269
    Posted Date: Saturday, April 1, 2006
  • Deleting Session When User Leaves Your Page
    This tutorial will show you how to delete sessions when a user closes the browser.
    Author: Chris
    Views: 12,153
    Posted Date: Tuesday, July 18, 2006
Download the EasyCFM.COM Browser Toolbar!