In my own experience, I have found that I find it easier to learn something if I actually work with it hands-on. Just reading a book or documentation doesn’t give you the practical knowledge you need to be able to rush off and start creating applications.
In this article, I will discuss the creation of a very simple application. I will cover what the application will do, how we will approach writing it, demonstrate in code how we will do it, and discuss additional features that could be added to it.
Users On Your Website
For my example, I have chosen an application that I wrote very early in my career as an ASP developer. It is intended to compliment a much larger application, but will work fine for our purposes.
The application we will write will allow you to list the users currently on your website. This project was a small addition to an already existing website that had a large following of regular users. I designed code that would allow these users to know who else was on the website while they were visiting.
When I first wrote this application, being a new ASP programmer, it was much more cumbersome than I would have written it now. I will illustrate some shortcuts you can use to make programming your ASP pages a little easier.
Dissecting The Needs For The Application
Now that we know exactly what it is we are going to be working on, we need to determine what pieces we need. I find it easier to put together my applications if I break down the pieces that are required into a list. This way, when I write one piece of the application, I will take the other pieces into consideration so they can work together seamlessly.
So, for our application we will need:
- For starters, the people who visit the webpage will need some way to type in their name so the others will know them. For our purposes, we will create a login page for this.
- We will need some place to keep the list of the current users online. We will use an access database.
- We will need code that will display the names.
- Finally, we will need a way to update the list of names when a user leaves the website.
Login Page
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.
Showing The List Of Users
Now that the users names are being added to the database when they login to your website, now we need some method to show them on the screen. Below is a sample web page that will display the list of users. It merely runs through all of the entries in the UsersOnline table and displays them to the screen.
showusers.asp
<%@ LANGUAGE="VBSCRIPT" %>Users currently on the website:
<HR>
<%
dim conn
dim strsql
dim mycount
‘ Create a SQL statement to insert the name into our table
strsql = "select UserName from UsersOnline”
‘ Open the database using our DSN
strconn = “DSN=UserBase”
set conn = server.createobject("adodb.connection")
conn.open strconn
‘ Open the recordset containing user names
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open strsql, conn, 3,3
‘ Set the mycount variable to contain the number of users in the database
mycount=rs.recordcount
if mycount < 1 then
‘ There were no users logged in
response.write “None”
else
for a = 1 to mycount
‘ Write out the user names
response.write(rs(“UserName”))
if a < mycount then
rs.movenext
end if
next a
endif
‘ Close the recordset
rs.close
set rs = nothing
‘ Close the database connection
conn.close
set conn = nothing
%>
The above example mixes HTML and VBScript together to achieve the desired results. As long as you keep the Scripting code within the <% and %> delimiters you can utilize both interchangeably within the same ASP page.
Simplifying The Code
The ASP page for showing the users is functional as shown in the previous section. But it can be pretty difficult to modify the look and feel of the page when you have a hearty mix of script and HTML. And what if you want to show the users on more than one page? Do you want to type the same code over and over again?
One method of simplifying your ASP pages is to use include files and functions. Include files are other basically other files containing HTML and/or script that you can reference from your ASP page, and will be considered part of the ASP page when the page is rendered. In this method, you can put code that you want to use on more than one page in an include file, and reference the file from many ASP pages. Include files can also be used to hide complex code to make it easier to modify the design of the HTML on your web pages.
Here is another copy of the showusers.asp page, this one using an include file with a function to show the users:
<%@ LANGUAGE="VBSCRIPT" %><!-- #INCLUDE FILE='functions.inc' -->Users currently on the website:
<HR>
<%
ShowUsers
%>
And here is the functions.inc:
<%
Sub ShowUsers()
dim conn
dim strsql
dim mycount
‘ Create a SQL statement to insert the name into our table
strsql = "select UserName from UsersOnline”
‘ Open the database using our DSN
strconn = “DSN=UserBase”
set conn = server.createobject("adodb.connection")
conn.open strconn
‘ Open the recordset containing user names
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open strsql, conn, 3,3
‘ Set the mycount variable to contain the number of users in the database
mycount=rs.recordcount
if mycount < 1 then
‘ There were no users logged in
response.write “None”
else
for a = 1 to mycount
‘ Write out the user names
response.write(rs(“UserName”))
if a < mycount then
rs.movenext
end if
next a
endif
‘ Close the recordset
rs.close
set rs = nothing
‘ Close the database connection
conn.close
set conn = nothing
End Sub
%>
As you can see, all of the working code has been put inside the functions.inc file in a subroutine called “ShowUsers”. The ASP page has become very compact and easy to read and modify. In addition, the functions.inc file can be included on any ASP page in your application, and used to display the users wherever you would like.
Removing Users From The List
Now we have a way to add users to the list, and to show the list of users on any web page. But at this point, we have not discussed a way to remove users from our list. Without removing the users, their names would stay on the list forever.
In order to remove users from the list, we will need to utilize the GLOBAL.ASA file. I am not going to go into great detail about the features of this file in this article. One of the features of the GLOBAL.ASA is the ability to recognize when a user’s session ends. This can be from the user choosing an option to logout of the system, or from a system-defined timeout.
Here is the GLOBAL.ASA for this:
<SCRIPT language=vbscript runat="server">
Sub Session_OnEnd
dim conn
dim strsql
‘ Create a SQL statement to insert the name into our table
strsql = "select UserName from UsersOnline”
‘ Open the database using our DSN
strconn = “DSN=UserBase”
set conn = server.createobject("adodb.connection")
conn.open strconn
‘ Use the session variable to delete the user from the table
strsql = “delete from UsersOnline where UserName = “ & chr(39) & _
Session(“UserName”) & chr(39)
conn.execute strsql
conn.close
set conn = nothing
End Sub
As you can see, I used the session variable that was created in the dologin.asp page to delete the user from the table when the session ends.
Additional Enhancements
That completes the rudimentary application. This code will functionally achieve the results that we started out with. For the sake of readability, it has been left very simple. If you were to wish to use this code in an actual application, there are many enhancements you could and some that you really should add. For example:
• Error handling for database connectivity issues.
• Unique identification column in the UsersOnline table. As the code is now, if there are two people with the name “Rich” logged in, when the first one logs out of the system, both of them are removed from the table.
• Login times could be also saved and displayed.
• Application_OnStart and Application_OnEnd functions could be utilized to clean out the UsersOnline table when the server stops and starts again.
• Database logic could be fully enclosed within include files.
• Logic could be added to the ShowUsers subroutine to suppress the current user’s name, so you wouldn’t see your own name in the list.
So there you have it. We have just designed and created an actual ASP application. Of course, what we made does not encompass a full website, but it adds the ability to have a list of active users on your website. I hope that reviewing this code helps those of you who are just getting started in ASP to better visualize the concepts, and puts you on the road to creating your own ASP applications a little quicker.
About the author:
Rich Smith owns and operates
Jamsoft Development, a programming firm who specializes in custom systems for small businesses.