DeveloperBarn Forums

Go Back   DeveloperBarn Forums > Programming & Scripting > Code Samples > Classic ASP

Discuss "Basic Login Script (using MS Acesss)" in the Classic ASP forum.

Classic ASP - Post your Classic ASP code samples here.


Reply « Previous Thread | Next Thread »  
 
LinkBack (1) Thread Tools Display Modes
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old March 24th, 2008, 04:50 PM
BLaaaaaaaaaarche's Avatar
Moderator

 
Join Date: Mar 2008
Posts: 43
Thanks: 10
Thanked 6 Times in 4 Posts
Rep Power: 1
BLaaaaaaaaaarche is on a distinguished road

Awards Showcase
Classic ASP 
Total Awards: 1

Default Basic Login Script (using MS Acesss)

Here is a sample of a basic login script for your website. You will find just one page. The form simply submits the page to itself to verify that the user information is accurate and the user email (or name) matches the password for that account.

The following is simply the HTML form that you will need for the users to fill out their information. You can change the field names or how the form works, this is just an example of the form:

Code:
<form name="login" method="post" action="">
  <table width="400" border="0" cellspacing="0" cellpadding="2">
    <tr>
      <td>Email Address</td>
      <td><input name="UserEmail" type="text" value="<%= UserEmail %>" /></td>
    </tr>
    <tr>
      <td>Password</td>
      <td><input name="UserPassword" type="password" value="<%= UserPassword %>" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Login" /></td>
    </tr>
  </table>
</form>
This part of code is the section that will actually authenticate the user against the values stored in the database. We will break it down piece-by-piece.

You will need this function to help stop SQL injection by replacing the single quotes with two single quotes:

Code:
'-- Simple function to replace single quotes --
Function ValidateStr(strValue)
	strTemp = strValue
	strTemp = Trim(strTemp)
	strTemp = Replace(strTemp,"'","''")
	ValidateStr = strTemp
End Function
Next, you will want to validate that the form has been submitted. Without this, the page will not know whether or not the form has been submitted.

Code:
'-- Check that form has been submitted --
If Request.Form("Submit") = "Login" Then
	'-- Grab form values --
	UserEmail = ValidateStr(Request.Form("UserEmail"))
	UserPassword = ValidateStr(Request.Form("UserPassword"))
After setting your requested values to variables, make sure that they both exist. If not, display error:

Code:
	' -- Check if both email and password were submitted -
	If UserEmail = "" OR UserPassword = "" Then
		strError = "You must enter both an email address and password."
	End If
If no errors, you will then need to open your database connection and query the databse with the submitted email and password.

Code:
	' -- If no errors, continue --
	If strError = "" Then
		'-- Connect to DB and create recordset --
		Set conn = Server.CreateObject("ADODB.Connection")
		conn.Provider = "Microsoft.Jet.OLEDB.4.0"
		conn.Open Server.MapPath("login.mdb")
		Set rsLogin = Server.CreateObject("ADODB.recordset")
	

		'-- Select the data from the DB using the submitted information --
		strSQL = "SELECT UserID, UserEmail, UserPassword FROM tblUsers WHERE UserEmail = '" & UserEmail & "' AND UserPassword = '" & UserPassword & "'"
		rsLogin.Open strSQL, conn
Check if the recordset is empty (if a record exists for the given email and password).

Code:
		' -- Check that user exists --
		If Not rsLogin.EOF Then
			'-- If match found, and user exists, then set session variable --
			Session("UserID") = rsLogin("UserID")
			' -- Redirect to protected page --
			Response.Redirect "profile.asp"
		Else
			strError = "Login failed."
		End If
	End If
End If
There, you have now successfully validated a login attempt. If you noticed, we stored the error message in a variable called strError. You can reference the error anywhere in your page by using the following code:

Code:
<%= strError %>
This variables contains the exact error why the login failed. This variable will only be populated if an error occured.

Once the user is logged in, you can reference the user ID by pulling the value from the session:

Code:
UserID = Session("UserID")

Comments on this post
don94403 agrees: Nice template. God! It's so much simpler in PHP.
Attached Files
File Type: zip login.zip (9.9 KB, 10 views)
Reply With Quote
The Following User Says Thank You to BLaaaaaaaaaarche For This Useful Post:
jmurrayhead (June 5th, 2008)
Sponsored Links
Reply

  DeveloperBarn Forums > Programming & Scripting > Code Samples > Classic ASP

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

LinkBacks (?)
LinkBack to this Thread: http://www.developerbarn.com/classic-asp/66-basic-login-script-using-ms-acesss.html
Posted By For Type Date
ASP Poll - ASP Free This thread Refback April 23rd, 2008 04:32 AM


Sponsored Links

ASP.NET Resource Index
a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer.

Free Web Directory
Including Chats and Forums Resources, Offer automatic, instant and free directory submissions.
URLZ Web Directory
URLZ Web Directory

Free Web Directory - Add Your Link
The Little Web Directory
Free Web Directory
Pegasus free web directory is a free directory organised by categories.

Web Directory & SEO Services
dirroot web directory


All times are GMT -4. The time now is 07:44 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Copyright © 2008 DeveloperBarn.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46