Continued from page 1
A few things. In order to use a database through ASP, you need to have a DNS created for that database on 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 DNS we created above
Dim toDatabase 'To connect to DNS Dim toRecordset 'To connect to individual tables
Set toDatabase = Server.CreateObject("ADODB.Connection") toDatabase.Open "customers"
Set toRecordset = Server.CreateObject("ADODB.Recordset") toRecordset.Open "logins", toDatabase, 2
' 2 = Opens recordset in "Write Mode"
' Let us say "logins" is some table you created in 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 email already exists in database. noError="n" end if
toRecordset.Close
Set toRecordset = Nothing
toDatabase.Close
Set toDatabase = Nothing
if noError="y" then ' If info was saved smoothly.
session("email")=sEmail session("pass")=sPass end if
' Here you can display some message that record has been saved. %>
This saves login information of a new customer. Now, how do we use it in future? First, 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 top of page, along with other ASP commands, include this too:
<% response.buffer=true %>
This is required if you want to send 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 DNS we created above
Dim toDatabase 'To connect to DNS Dim toRecordset 'To connect to 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 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 user is logged in, just check value is session("email"), like:
<%
if session("email")<>"notlogged" then
' do things for logged in customer
else
' tell 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.
Amrit Hallan is a freelance web designer. For all web site development and web promotion needs, you can get in touch with him at http://www.bytesworth.com. For more such articles, visit http://www.bytesworth.com/articles and http://www.bytesworth.com/learn You can subscribe to his newsletter [BYTESWORTH REACHOUT] on Web Designing Tips & Tricks by sending a blank email at bytesworth-subscribe@topica.com