Coldfusion Login Script

Here i am going to show you how to make a login script that will check users against a database for the user name and password.

First of all we will need to make a login form, this is just going to be a basic form with a user name field, password field and a login button.

<form id="Login" action="login_action.cfm" method="post" name="Login">
<table cellspacing="0" cellpadding="0" width="300" align="center" border="0">
<tbody>
<tr>
<td colspan="2">
<div class="style2" align="center"><strong>Login</strong></div>
</td>
</tr>
<tr>
<td>Username</td>
<td><input id="Username" name="Username" type="text" /></td>
</tr>
<tr>
<td>Password</td>
<td><input id="Password" type="password" name="Password" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit" /></td>
</tr>
</tbody>
</table>
</form>

Now we have the form we need check the form and see if the user exists and that there password is correct. If the details are not correct they will be sent back to the login page.

If the details are correct then we will set there user name as a session variable so that it can be used in the site, and we will send them to the secure page.

<cfquery datasource="Users" name="Login">SELECT * FROM users WHERE username = '#FORM.username#' AND password = '#FORM.username#' <cfif is="" login.recordcount=""><cflocation addtoken="No" url="login.cfm?badlogin=1"><cfelse><cfset session.loggedin="1"><cfset session.username="#Login.username#"><cflocation url="/index.cfm">

As you can see this is a very basic script, but it is all you need to get started. I will soon be covering how to store encrypted passwords in your database and how to add roles to the users.

Related Blog Entries

Comments
Esmeralda's Gravatar What about using labels and possibly CSS for styling since it is a simple form?
# Posted By Esmeralda | 27/10/08 14:15
Jonny Shaw's Gravatar well this post is more about the login script than the form
# Posted By Jonny Shaw | 27/10/08 17:08
Patti's Gravatar Thank you for explaining this - just what I was looking for!
# Posted By Patti | 06/11/08 17:39

Archives By Subject

Advertising (4) [RSS]
Blog Design (1) [RSS]
CFProject Scripts (2) [RSS]
Coldfusion Charts (3) [RSS]
Coldfusion Functions (5) [RSS]
Coldfusion Overview (1) [RSS]
Coldfusion Tutorials (17) [RSS]
For Sale (2) [RSS]
Image Manipulation (1) [RSS]
Java (1) [RSS]
JavaScript (4) [RSS]
PHP (1) [RSS]
Railo (2) [RSS]
SQL (1) [RSS]
Useful Tools (3) [RSS]

Recent Comments

Hashing Passwords
Richard Lord said: You really need to salt the passwords before hashing them, otherwise you're exposed to a rainbow-tab... [More]

Using Railo and Resin with domain aliases
Jonny Shaw said: Thanks for that. I am already using <host regexp="(.+)"> <host-name>${ho... [More]

Using Railo and Resin with domain aliases
Gert Franz said: Hi Jonny, it's even easier if you do it this way: <host id="allmead.com" root-dir... [More]

BlogCFC on Railo
Jonny Shaw said: Thanks for the advice ray. I was surprised at how easily BlogCFC went accross to to railo. The onl... [More]

BlogCFC on Railo
Gert Franz said: Congratulations to your new websites. I just wanted to add two things: CFIMAGE and CFCHART are suppo... [More]

Recent Entries

No recent entries.
ColdFusion Blog | ColdFusion Hosting | ColdFusion Q & A