Writing Your First ASP Application ( Quick Start ) - Login Page
(Page 4 of 8 )
Before we can create the login page, we will need the database in which to keep our names. One of the easiest databases to work with is Microsoft Access. For our example, we will assume that I have created a database called “UserBase”. In this database, I have a table named “UsersOnline”, with one column named “UserName”. For ease of discussion, I have kept this database very simple, but more tables and columns could be added to add more functionality to the application. I have also created a DSN called “UserDSN” to make connectivity to the database simple. I will not go into the details of database connectivity in this article.
Now that I have a database, I need to create a login page. This could be a page by itself, or just a textbox and button within an even larger web page. Here is the code for our login page.
login.asp:
Please enter your name to login
.
<FORM action=”/dologin.asp” method=”post”><INPUT name=username> <INPUT type=submit value=Submit name=B1> </FORM>
When the user types in their name and clicks the submit button, that information will be passed to the dologin.asp page. This program’s job will be to take the name that was entered and manually add it to the table.
dologin.asp:
<%@ LANGUAGE="VBSCRIPT" %><%
dim conn
dim strsql
dim username
‘ Read the username from the form data
username = trim(request.form("UserName"))
‘ Create a session variable that contains the user name
Session(“UserName”) = username
‘ Create a SQL statement to insert the name into our table
strsql = "insert into UsersOnline (Username) values (“ & chr(39) & username & chr(39) & “)”
‘ Open the database using our DSN
strconn = “DSN=UserBase”
set conn = server.createobject("adodb.connection")
conn.open strconn
‘ Execute the SQL Statement to insert the record
conn.execute strsql
‘ Close the database connection
conn.close
set conn = nothing
%>Login Successful You have successfully logged in to our system.
There are a few things I would like to point out at this point. First, you will notice that all of the VBScript code is contained within <% and %> delimiters. All VBScript must be within these delimiters or the ASP page will not load. I also used the function chr(39) to represent a quotation mark inside the SQL statement.
Next, you will notice that I did not use any error checking. For readability purposes, I have kept the code as clean and simple as possible. For a real-world scenario, appropriate error handling and checking would be applied.
I also made use of a session variable to save the username. Each unique user to the website is assigned a session, and each variable you set within the session is unique to that user only. So if five people logged in with different names, each would have his or her own session Ids.
Finally, I am using ADO to connect to the database. Using ADO for database access is a whole topic unto itself, and I will not go into great detail about it here in this article.
Next: Showing The List Of Users >>
More ASP Articles
More By Rich Smith