View Single Post
Old 03-11-2010, 01:21 AM   PM User | #5
Gjslick
Regular Coder

 
Join Date: Feb 2009
Location: NJ, USA
Posts: 476
Thanks: 2
Thanked 70 Times in 69 Posts
Gjslick will become famous soon enough
Hey, implementing a check against an access database for users is pretty easy. First thing is first though, do you have access to the ColdFusion administrator? If you don't, you need to figure out how to set up a Data Source with whatever host you are using.

If you do have Administrator access, then you first need to set up a Data Source for the Access database. ColdFusion needs to have all of the details for connecting to the database first, before you can query it in code. All of that information (file location, connection credentials, etc) is stored under a single "Data Source Name", so that it can all be used in multiple places in your code, and if you ever need to change that information, you only have to change it in one place.

So go into the ColdFusion Administrator, and go under "Data & Services" -> "Data Sources" (on the left menu). Enter a Data Source Name for your Access database, and select the Microsoft Access driver. If this Access database only holds users, you can name the Data Source something like "userDB". If it holds all of your website's data, you'll want to name it something more general, such as "siteDB" or even just "db". Once you click "Add", you'll be asked to locate the database file on the server, and for any credentials that ColdFusion will need to open it. I don't believe that you need to specify a "System Database File" by the way, and all of the "Advanced Settings" are probably fine by default.

Once that's all set up, you can query the database (using the Data Source) to check user credentials. Here's an example login_process.cfm with a query. This example assumes that you have a table named 'users' inside of the database, and that the users table has fields: 'username' and 'password'. It will check for a record with a matching username and password sent from the login form, and if it finds one, will let the user in.
Code:
<cfquery name="checkUser" datasource="userDB">
    SELECT * FROM users
    WHERE username = '#form.username#'
        AND password = '#form.password#'
</cfquery>

<cfif checkUser.recordCount eq 1>
    <!--- A user record was found for the username/password, log them in --->
    <cfset session.loggedIn = true>
    <cflocation url="memberWelcome.cfm">
<cfelse>
    <!--- A user record was not found for the username/password, send them back to the login page --->
    <cflocation url="login.cfm?invalidLogin=true">
</cfif>
I highlighted the data source name in red. That will be whatever you named it in the Administrator.

Hope that helps, and let me know how it goes!

-Greg
Gjslick is offline   Reply With Quote