Secure Login

Posted on 2006-07-14
Last Modified: 2013-12-24
I am pretty new to Cold Fusion and I want to use the application.cfm to check if the user logged in.  I've been trying to figure it out and I just can't.  Here is the code to my login page.  The user login and password is in a table called tbl_QA_staff.  The fields are strUserID and strPassword.

            <cfform name="loginform" action="QAQ_home.cfm" method="post">
              <table width="80%" cellspacing="2" cellpadding="2" align="center" bgcolor="#C0C0C0">
                  <td colspan="2" align="center" class="small"> Please enter your
                    Employee login and password.<br> <img src="../images/dot_clear.gif" width="1" height="10">
                  <th align="right" class="normal"><b>Login:</b></th>
                    <td><cfinput type="text"
                  <th align="right" class="normal"><b>Password:</b></th>
                  <td><cfinput type="password"
                  <td colspan="2"><img src="../images/dot_clear.gif" width="1" height="15"></td>
                  <td colspan="2" align="right"><input type="Image" src="../images/go.gif" name="login_user" value="Log In"></td>
            </cfform> </td>

At this point once they hit the button and go to QAQ_home.cfm.  I use a simple query and recordcount to check if the user exists.  Here is the code for that


      <CFQUERY name="getUser" datasource="QAQ">
      SELECT *
      FROM TBL_QA_Staff
      WHERE strUserID = '#form.username#'
      AND   strPassword = '#form.password#'      

      <!-- Verify User and set up sessions -->
      <CFIF getUser.RecordCount gt 0>
            <cfset dt = #dateformat(CreateODBCDate(now()),"mm/dd/yyyy")#+1>
            <cfset Session.UserName = getUser.strQA_name>
            <cfset Session.UserID = getUser.QA_ID>
            <cfset Session.UserLogin = getUser.strUserID>
            <cfset Session.UserLoc = getUser.StrQA_loc>
      <!--- BEGIN User does not exist --->
      <cflocation url="#loginpage#">

I was told that this is not the most secure way to do things and that I should authenticate the user in the application.cfm.  My question is how do I do that?
Question by:trifecta2k
  • 3
  • 2
LVL 13

Accepted Solution

usachrisk1983 earned 500 total points
ID: 17111383
Inside your application.cfm file, do something like this:

<cfif not isDefined('session.UserName') and ListLast(cgi.script_name,'/') neq 'login.cfm'>
   <cflocation url="login.cfm">

This will cause login.cfm to load if session.username doesn't exist.  Use login.cfm to authenticate the user (you can use it as both the form page and the action page), and set session.username when you've authenticated them.  Now each page of your site will make sure the user is logged in before allowing them to view the page.

Author Comment

ID: 17111569
How do I use the login.cfm as teh form and the action page?  I'm sorry I'm just new to this and I don't know how to do that.  
LVL 13

Expert Comment

ID: 17111619
No problem :)  Something like this should work for you:

<cfif isDefined('form.username')>

  <!--- The user has submitted the form --->
  <cfquery name="x" datasource="y">
    select something from somewhere
  <!--- Verify the user has access to login, and set your session variable>


  <!--- Show the form here, either exclude the ACTION or set it back to the same page. --->


Author Comment

ID: 17111720
Seems like everything works.  Before the <CFELSE>  I added a <CFLOCATION> to move the application along and it works just fine.   I have one last question.  This is something new that I just noticed.  How so I get rid of the session variables.  They seem to never delete.

Author Comment

ID: 17111915
Nevermind, I got it. Thanks for all your help.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Script 12 133
Need to redirect request from IIS server to old Apache Server. 3 89
JKS to store upstart data 2 84
New OSQA server has a ton of fake users 4 53
This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
A short film showing how OnPage and Connectwise integration works.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now