Use Database
First you need to create a database. Let's use MS Access. Make sure
you have MS Access installed on your computer and you know how to configure
it.
in Windows 95/98. you should go into the ODBC32 icon in your control
panel, and add MS Access Drive on System DSN. Then create a database and
a table. You must give a name to your database and to the table. Let's
name the database "my_db.mdb", name the table "members"
and the path to this database is:
C:\Inetpub\wwwroot\my_db.mdb
Let's design the table something like this:
userId |
user |
pass |
page |
00234578 |
John |
John123 |
john.asp |
-11350986 |
Mike |
Mike456 |
mike.asp |
98782345 |
Joe |
Joe789 |
joe.asp |
... |
... |
... |
... |
To open the database, we need the following code.
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Driver={Microsoft Access Driver (*.mdb)};
DBQ=C:\Inetpub\wwwroot\my_db.mdb"
Now you have established a connection to the database. To get data from
the database, we can use SQL.
The next statement is combined ASP code and SQL:
sqlStr = "select * from members"
set RS = Conn.Execute(sqlStr)
You can write records from the database to your web browser;
while not RS.eof
Response.write "User name: " & RS("user") & "<br>"
Response.write "Password: " & RS("pass") & "<br>"
Rs.movenext
wend
"eof" stands "end of file". So the meaning of the
above code is: if the search RS does not reach the end of the table, keep
writing the records on web browser. When you execute the script, all the
records of "user" and "pass" will be retriaved. The
below is a complete ASP login page using database. Let's save it as second.asp:
<%
u=request("user")
p=request("pass")
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Driver={Microsoft Access Driver (*.mdb)};
DBQ=C:\Inetpub\wwwroot\my_db.mdb"
sqlStr = "select * from members where "
sqlStr = sqlStr & "user='& u &"' and pass='&
p &"'"
set RS = Conn.Execute(sqlStr)
if not RS.eof then
Response.redirect " & RS("page")
else
Response.write "Sorry, invalid login"
end if
%>
The basic idea in this script is that when an user login from first.asp,
the query strings of user name and password are sent to second.asp. The
ASP opens database and selects records according to the query strings.
If the records are fond, the ASP redirects page to a specific page. If
there are no records match the query strings, then display a message and
tell the user that the login was invalid. |