Logging,Using,ASP,Access2000,A computer Logging In Using ASP - Access2000
Gone are those times when the companies and the organisations didn't need a hi-tech system to handle them. Owing to the considerable increase in the business sector and thus, an enormous increase in the complexity of the organisational struc ----------------------------------------------------------Permission is granted for the below article to forward,reprint, distribute, use for ezine, newsletter, website,offer as free bonus or part of a product for sale as longas no changes a
After receiving a few queries about how to store passwords usingACCESS and ASP, and then use them as "logins", I thought, well,why not write in a separate article, instead of attachingmultiple ASP files that are full of confusing comments andvariables only to be decipherable by my brain?I'm assuming you've installed, and are running PWS (Personal WebServer) on your machine, if you are not already working on aserver that supports ASP.First of all, create a database, for instance, customers.Define atable with all the fields you require (include email andpassword).After the database has been created, you need to create a DNS inorder to access this database through your ASP pages.If you have never created it, this is how you do it:Go to the Control Panel (My Computer -> Control Panel), and clickon the icon that should be saying "ODBC Data Sources (32bit)". Inthe resulting window, select the "System DSN" tab. Then click onthe "Add..." button. From the given list of Database drivers,select "Microsoft Access Driver (*.mdb)" and click the "Finish"button. You reach a place where you have to enter the "DataSource Name". Enter it, anything, for instance, "customers". Thenclick the "Select..." button. This lets you select the Accessdatabase you created. Press Ok, press Ok, and press Ok. Your DSNis created.In the first part, I'll write about storing the passwords.Before this, let's make an include file to create and initializethe session variables that we are going to need (we can usecookies, but some clever folks disable cookies on theirbrowsers).File name: sessions.inc<%if session("email")="" then session("email")="notlogged" session("pass")=""end if%>This file you can include in every page asError processing SSI file
so that you can use them whenever you need them.Now accepting login and password.For this you require a normal HTML form. You can have "n" numberof fields in a form, but here, our primary concern is, gettingthe email as login, and the accompanying password.Here's the form:Please enter your details:We validate the form before it proceeds to the "action" file sothat there is very little server-side processing. A simplevalidation:Note: Put the following Javascript above the tag.So now when the user clicks on "Submit", he/she goes to"storelog.asp" In between, you can have a file to confirm theform fields and give the user an option to modify them beforefinally saving.A few things. In order to use a database through ASP, you need tohave a DNS created for that database on the server.STORELOG.ASP should somewhat look like this:<%dim sEmail, sPass, noErrornoError="y"sEmail=request.form("email")sPass=request.form("pass")' The following lines setup a connection to the DNS we createdaboveDim toDatabase 'To connect to the DNSDim toRecordset 'To connect to the individual tablesSet toDatabase = Server.CreateObject("ADODB.Connection")toDatabase.Open "customers"Set toRecordset = Server.CreateObject("ADODB.Recordset")toRecordset.Open "logins", toDatabase, 2' 2 = Opens the recordset in "Write Mode"' Let us say "logins" is some table you created in the database.toRecordset.AddNewtoRecordset("email")=sEmailtoRecordset("password")=sPasson error resume nexttoRecordset.Updateif err.number<>0 then' do something if some error occurs.' one error could be that the email already exists in thedatabase. noError="n"end iftoRecordset.CloseSet toRecordset = NothingtoDatabase.CloseSet toDatabase = Nothingif noError="y" then' If the info was saved smoothly. session("email")=sEmail session("pass")=sPassend if' Here you can display some message that the record has beensaved.%>This saves the login information of a new customer. Now, how dowe use it in the future? First, the login form, that could be onany page.Remember you can use somewhat same validation Javascript heretoo, so I'm not repeating it, but just mentioning it.Please login by entering your email and password.LOGIN.ASPAt the top of the page, along with other ASP commands, includethis too:<% response.buffer=true %>This is required if you want to send the user to some page afterhe/she has successfully logged in.<%dim sEmail, sPass, noErrornoError="y"sEmail=request.form("email")sPass=request.form("pass")' The following lines setup a connection to the DNS we createdaboveDim toDatabase 'To connect to the DNSDim toRecordset 'To connect to the individual tablesSet toDatabase = Server.CreateObject("ADODB.Connection")toDatabase.Open "customers"fndSQL="select * from logins where email='" & sEmail & "' andpassword='" & sPass & "'"Set toRecordset=toDatabase.execute(fndSQL)if toRecordset.eof then response.write "Your details are not in the database, pleasetry again, or register yourself."else session("email")=toRecordset("email") session("pass")=toRecordset("password")end iftoRecordset.CloseSet toRecordset = NothingtoDatabase.CloseSet toDatabase = Nothingresponse.redirect "To some URL"%>>From now onwards, whenever you want to perform some action thatshould only be performed if the user is logged in, just check thevalue is session("email"), like:<%if session("email")<>"notlogged" then' do things for the logged in customerelse' tell the customer that he she is not logged in.end if%>Hope this helps. If you need further queries, or in future youneed some other ASP work, you are welcome to write to me [email protected].
Logging,Using,ASP,Access2000,A