The visitor.asp Page in Action

As mentioned, the visitor.asp form will be called when the user clicks the button on the previous form. This next form will grab the hidden text from the cookie, look up the number in a database, and determine if this user has been here before. If the user is in our database already, the visitor.asp screen will actually be displayed when they log in. Because our code checks and finds a valid cookie, the information for that user can be retrieved. After the user has logged into our site at least once, they will see the visitor.asp form:

So here, the database successfully retrieved the user. But if no cookie is found, our program assumes that this is a new client. Of course, if the user deletes the cookie, or refuses to accept it, our program has no way to track them. But if this is the first time someone logs into your site, here is what they will see:

OK, let's see how this is done. This form will take a bit of work because we are going to create a database to hold user information. We will use ADO to manage the database. However, we are going to encapsulate all of the data management into a dynamic link library (DLL) file. This file can then be called upon just as if it were part of our program. Let's do it!

Try It Out - The visitor.asp page – Our Client-Server Application's Workhorse

1.  Open Notepad and add this code. Save the file in \Chapter13 as visitor.asp:

<%
visitorCookie = Request.Form("cookieValue")
if visitorCookie = "" then
  response.Redirect "newUser.asp"
End If
%>

<HTML>
<HEAD>
<TITLE>Database Programming with Visual Basic 6.0</TITLE>
</HEAD>

<BODY>
<CENTER>
<H1><font size=4>Retrieving Visitor Information</font></H1>
<H2>Database Programming with Visual Basic 6.0</H2>
</CENTER>
<BR>
<B>

<%
Dim myDll
Dim myArray 
dim firstName
dim lastName
dim previousVisit
dim totalVisits
dim secondsAgo

Set myDll = Server.CreateObject("trackVisitors.visitors")

myArray = myDll.getvisitor(visitorCookie)

firstName = myArray(0)
lastName = myArray(1)
previousVisit = myArray(2)
totalVisits = myArray(3)

Response.Write("Welcome back ")
Response.Write(firstName)
Response.Write(" ")
Response.Write(lastName)
%><P>
<%
Response.Write("You have visited my web site: ")
Response.Write(totalVisits)
Response.Write(" times.") 
%><P>
<%
Response.Write("The last time you were here was ")
Response.Write(previousVisit)
%>
<P>

</B>
<HR>
<h5>Copyright: Programming Databases with Visual Basic 6.0.<br>
Last revised: May 24, 1998</h5>
</BODY>
</HTML>

How It Works

As with reading and writing cookies, the .Redirect method of the Request object must be called before any HTML code. So we check for the value of cookieValue from the hidden text box, cookieValue, in the login.asp form. If there is no cookie value we know this is a new surfer to our site. In this case, we simply redirect control to the .asp file we use to get information from the user. Namely, we pass control to the newUser.asp form we just examined. If however, the user has a valid cookie, our code continues normally:

<%
visitorCookie = Request.Form("cookieValue")
if visitorCookie = "" then
  response.Redirect "newUser.asp"
End If
%>

If the user indeed has a cookie on their machine, we want display the header on the form and to instantiate an instance of our DLL. We will write this .DLL file as soon as we finish reviewing how this form works. But as you can see, we set a reference to our DLL with the object variable myDLL. And our DLL will handle all of the ADO data access:

Set myDll = Server.CreateObject("trackVisitors.visitors")

Next, we call the .getVisitor method of our DLL. We pass in the visitorCookie which holds the unique id of this particular user. The DLL retrieves the user record and places the information in a variant array. We retrieve the array of information from our DLL in an array we call myArray.

myArray = myDll.getvisitor(visitorCookie)

Next, we extract the fields from the array that is returned from our DLL and place the element data in our variables for easy display:

firstName = myArray(0)
lastName = myArray(1)
previousVisit = myArray(2)
totalVisits = myArray(3)

Now we just write the information to the page normally:

Response.Write("Welcome back ")
Response.Write(firstName)
Response.Write(" ")
Response.Write(lastName)

That's it for the VBScript code. Now we need a database to work with! Let's start with building a customized database to hold the information on all of the hundreds of thousands of surfers that will be visiting our site every day.

© 1998 by Wrox Press. All rights reserved.