|
After receiving a few queries about how to store passwords using We validate the form before it proceeds to the "action" file so that there is very little server-side processing. A simple validation: 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 the form fields and give the user an option to modify them before finally saving. A few things. In order to use a database through ASP, you need to have a DNS created for that database on the server. STORELOG.ASP should somewhat look like this: <% dim sEmail, sPass, noError noError="y" sEmail=request.form("email") sPass=request.form("pass") ' The following lines setup a connection to the DNS we created above Dim toDatabase 'To connect to the DNS Dim toRecordset 'To connect to the individual tables Set 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.AddNew toRecordset("email")=sEmail toRecordset("password")=sPass on error resume next toRecordset.Update if err.number<>0 then ' do something if some error occurs. ' one error could be that the email already exists in the database. noError="n" end if toRecordset.Close Set toRecordset = Nothing toDatabase.Close Set toDatabase = Nothing if noError="y" then ' If the info was saved smoothly. session("email")=sEmail session("pass")=sPass end if ' Here you can display some message that the record has been saved. %> This saves the login information of a new customer. Now, how do we use it in the future? First, the login form, that could be on any page. Remember you can use somewhat same validation Javascript here too, so I'm not repeating it, but just mentioning it.Please login by entering your email and password. LOGIN.ASP At the top of the page, along with other ASP commands, include this too: <% response.buffer=true %> This is required if you want to send the user to some page after he/she has successfully logged in. <% dim sEmail, sPass, noError noError="y" sEmail=request.form("email") sPass=request.form("pass") ' The following lines setup a connection to the DNS we created above Dim toDatabase 'To connect to the DNS Dim toRecordset 'To connect to the individual tables Set toDatabase = Server.CreateObject("ADODB.Connection") toDatabase.Open "customers" fndSQL="select * from logins where email='" & sEmail & "' and password='" & sPass & "'" Set toRecordset=toDatabase.execute(fndSQL) if toRecordset.eof then response.write "Your details are not in the database, please try again, or register yourself." else session("email")=toRecordset("email") session("pass")=toRecordset("password") end if toRecordset.Close Set toRecordset = Nothing toDatabase.Close Set toDatabase = Nothing response.redirect "To some URL" %> >From now onwards, whenever you want to perform some action that should only be performed if the user is logged in, just check the value is session("email"), like: <% if session("email")<>"notlogged" then ' do things for the logged in customer else ' tell the customer that he she is not logged in. end if %> Hope this helps. If you need further queries, or in future you need some other ASP work, you are welcome to write to me at amrit@bytesworth.com.
|
| Programming | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||