



(6 ratings)
The process for counting visitors to a Web site is problematic. The difficulty is in deciding just Who is a visitor? or What is a visitor? or When is a visitor? On the surface it might sound easy. Simply tally a visitor whenever the person accesses a page at your site. However, this doesn't work because you end up over-counting the person who wanders from page to page. The same can be said about counting a visitor by restricting the tally to the "home" page. In all likelyhood this will be the most-visited page, even by the single visitor, and you'll end up counting this same person again and again. So, establishing a counter that increments with each visit to a page is fraught with difficulties.
An alternative is to identify each visitor uniquely and count the person once when they arrive at your site. This is a good idea but difficult to implement. First, you would need some mechanism to identify a new visitor, and then you would need to track them from page to page to make sure that, indeed, the person who accessed the page is the same person who's already been counted. This solution would involve a lot of processing overhead and a lot of passing of information from page to page.
This solution also fails to take into account the fact that visitors often leave a site and return a few minutes later. Do you count them again or do you count them only after they have been away for a fixed period of time? Now you need to include some sort of timing mechanism to help count visitors and you need to be able to associate particular timers with particular visitors. What a mess!
Fortunately, ASP's Session Object, described previously, is tailor-made to handle the visitor-counting problem. Recall that a Session is automatically established for each person who arrives at your site. The Session remains active as long as the person is browsing your site. And, the session ends after the visitor has been absent for longer than 20 minutes. By definition, a session is a visitor--a person who arrives at one of your pages, wanders around as much as they like, and is not gone for more than 20 minutes at a time.
We'll set up a visitor counter for our logon application. The count of visitors will be displayed on the welcome.asp page, after the person has logged on.
Establishing a Counter
Before setting up a Session Object to count visitors, we need to establish a way to tally the count. This can be done by creating a new Counters table in our database. This table needs to contain only one field (which we'll name VisitorCounter) and one record initialized to zero.
We've chosen here to establish a Counters (plural) table in case we have a later need to define other types of counters which can become other fields in the table. We need to define the VisitorCounter field as a Number and enter a zero value. We can, of course, initialize the counter to 1,000,000 to impress our friends and investors.
One note of caution. You actually have to type a "0" value into the field. When you open the table it appears as if there is already a record with a zero in the field. There is a single line in the table with a "0" value displayed. However, this is only a "potential" record. You need to type a "0" to establish the value for the record; then the table expands to two lines, the second of which is the next "potential" record. If you've done this correctly, the table should appear as shown above, with the indication that there is 1 Record in the table.
Counting a Visitor
Since visitor counting takes place when a person first arrives at our site, the script to count this person appears in the Session_OnStart subroutine of the global.asa file. Recall that this subroutine is run automatically when a session is first established, which is our definition of a new visitor. The script will add 1 to the VisitorCounter field in the Counters table. Here is that script, which appears along side the Session("PassCheck") variable that was coded previously to flag authorized visitors.
Sub Session_OnStart
Session("PassCheck") = False
'-- Add 1 to the Visitor Counter
Set CNObj=Server.CreateObject("ADODB.Connection")
CNObj.Open "DBQ=d:\Databases\Database.mdb;DRIVER=Microsoft Access Driver (*.mdb)"
CNObj.Execute "UPDATE Counters SET VisitorCounter = VisitorCounter + 1"
CNObj.Close
End Sub
</SCRIPT>
First off, we've added a comment to the script to identify the visitor counter routine. A comment is identified by the apostrophe ( ' ) character. Anything on the line following the apostrophe is considered a comment and is ignored by the ASP processor. Multiple lines are commented by placing an apostrophe at the start of each of the comment lines. The apostrophe does not have to appear as the first character on a line.
In order to link to our database we use the identical Connection Object and connection string we have used in previous scripts. These will remain identical as long as we are accessing the same database.
The Execute Method
There is a new statement used in the third line of the script. Its general format is show below.
|
ConnectionObject.Execute "SQL statement" |
Besides its Open method, the Connection Object supports the Execute method. This method is used to issue an SQL command to the database management system. When this method is used, it is not necessary to create a Recordset Object or to open a table using the Recordset Object's Open method. The Connection Object can take care of the processing through direct communication with the database management system. Besides, for this application it is not really necessary to "extract" a set of records from the Counters table and made it available for script processing. We just need to update a value that's in the table, which is something the DBMS can do for us with the proper SQL command.
The SQL UPDATE Statement
The SQL language has an UPDATE statement that is used to set the value of a data item in a table. Its general format is shown below.
|
UPDATE TableName SET FieldName1=value1 [,FieldName2=value2]... |
The word UPDATE is followed by the name of the table containing values to be set. Then, within the SET clause, there appears a list of field names and associated values that are to be placed in the table. These values can be literal values, numbers, variables, or arithmetic expressions.
For our particular application we wish to update the VisitorCounter field in the Counters table with a new value that is one larger than the current value in the field (add 1 to the visitor counter). So, the syntax is
UPDATE Counters SET VisitorCounter = VisitorCounter + 1This SQL command is then issued through our Connection Object's Execute method in the following fashion:
CNObj.Execute "UPDATE Counters SET VisitorCounter = VisitorCounter + 1"Again, if the SQL statement is fairly complex, you might wish to compose it first as a string variable and then execute the variable.
SQL = "UPDATE Counters SET VisitorCounter = VisitorCounter + 1"CNObj.Execute SQL
In the script we've also chosen to formally close the connection after issuing the SQL statement. This is not necessary, but it feels good.
Recordset versus SQL Methods
By now you are probably confused on whether or not a script needs to use a Recordset Object to accomplish its processing. This is really a matter of choice. Of course, the most efficient way to work with a database is through SQL commands, and there is a sufficient set of commands to accomplish nearly any processing task. On the other hand, it is sometimes confusing to construct an SQL command to perform complex processing. In this case you might choose to go with the more straight-forward recordset method.
When performing database maintenance activities--adding to, changing, or deleting records in a database--choose whichever method suits your purpose. Either issue an SQL statement directly through the Connection Object, or retrieve a recordset through the Recordset Object and iterate through the records with your script. On the other hand, when performing database displays--writing the contents of a database to a Web page--you are forced to use recordset methods. There is no SQL command to write to a Web page; so, you need to extract a set of records from the database (either using the SQL SELECT statement or opening the entire table) and iterate through the resulting recordset, displaying the values on the page.
There are no hard and fast rules to follow. As you gain experience, you will develop your own preferences for the methods to use. Any of these preferences are perfectly valid.
Displaying a Visitor Counter
Now that we have coded the mechanism to count visitors arriving at our site, we want to display this value on our welcome.asp page. Here is the code integrated into this page:
welcome.aspIf Session("PassCheck") = False Then
Response.Redirect("logon.asp")
End If
%>
html starts here
bod starts here
<h3>Welcome Page</h3>
<p>Welcome to my page.</p>
<%
Set CNObj=Server.CreateObject("ADODB.Connection")
CNObj.Open "DBQ=d:\Databases\Database.mdb;DRIVER=Microsoft Access Driver (*.mdb)"
Set RSObj=Server.CreateObject("ADODB.Recordset")
RSObj.Open "SELECT VisitorCounter FROM Counters",CNObj
Response.Write("<p>You are visitor #" & RSObj.Fields("VisitorCounter") & " to this site.</p>")
RSObj.Close
CNObj.Close
%>
html and bod ends here
Here we have a complete script appearing in the middle of the HTML page. The script is located where its results are displayed. First, we make a connection to our database and then create a RecordsetObject for use in extracting the VisitorCounter value from the Counters table. An SQL SELECT statement extracts this recordset, which is a single record with a single field. Thus, there is no need to iterate through this recordset since it contains only a single record to which the recordset cursor is pointing when it is opened. The RSObj.Fields("VisitorCounter") field contains the counter value, so the Response Object's Write method is used to display this value inside some descriptive text.
When this page is displayed, it looks like the following:
Welcome Page
Welcome to my page.
You are visitor #1000 to this site.
20 Random Tutorials from the same category :
ASP.NET in Dreamweaver 8
NEW WAY TO HANDLE VARIABLES
Searching for Matching Values
Processing Form Information In ASP
Passing Query Strings In Asp
Installing ASP on your own computer
Recordset Access Methods
The Recordset Object
ASP.NET 2 Special Purpose Folders
Randomizer for ASP
AIM Profile Screenname Viewer as a component of AIM Profile Designs
SQL Access Methods In ASP
Server Variables In ASP
Counting Visitors In ASP
Iterating through Collections In ASP
Date and Time Functions In ASP
DISPLAY DATA FROM DATABASE
Reading form variables passed in the URL
Counter in ASP
ASP.NET Tutorial - Making image thumbnails













