Databases and Cookies - Try It Out – Resetting a Cookie
(Page 11 of 13 )
Our objective is to show on the home page the visitor's name and professional class, based on the person's ID stored as a cookie. We will do that by first reviewing the home page from the last Try It Out, which does not have a cookie check. Then we will modify that home page to check for cookies. If there is no cookie, then the visitor will be shunted to ResetCookieForm.asp and that leads to ResetCookieResponse.asp.
One note on this exercise. In the course of this exercise you will probably want to delete a cookie for testing. After deleting the cookie it is best to re-start your browser.
You can delete a cookie by hand from IE by going to C:\Windows\cookies . It will probably be named UserName@SiteName . You can also sort by date and you will see your cookie as the most recently changed. In Netscape a cookie is located at: c:\program files\netscape\users\<profile name>\cookies.txt
The code for a typical home page that uses a cookie would look like:
<%@ Language=VBScript %>
<HTML><HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>2726-11-TI-O3 Reset Cookie Home Page No Check</TITLE>
</HEAD><BODY>
<H1>Welcome to the Sailor's Home Page</H1>
<P>(no cookie check)</P>
<%
Dim varPeopleID
Dim oRS
varID=Request.Cookies("PeopleID")
sqltxt = "SELECT PeopleNameLast, PeopleNameFirst, "
sqltxt = sqltxt & "PeopleProfessionalClass FROM People "
sqltxt = sqltxt & " WHERE PeopleID =" & varID & ";"
Set oRS = Server.CreateObject("ADODB.Recordset")
oRS.open sqltxt, "DSN=Sailors"
Response.Write "Welcome " & oRS("PeopleNameFirst")
Response.Write " " & oRS("PeopleNameLast") & "<BR>"
Response.Write "<P>We have you registered as "
Response.Write " of Professional Class "
Response.Write oRS("PeopleProfessionalClass")
%>
</BODY></HTML>
And the above code (available as file 2726-11-Cook-TI-03-ResetCookieHomePageNoCheck.asp) would produce the following page:

But when we consider that cookies may be unusable or unavailable we have to modify that home page to detect the absence of a cookie. If the cookie does not exist then the user is notified and asked to click to the Cookie Reset page, as follows. This code is available as file 2726-11-TI-03-ResetCookieHomePageWithCheck.asp:
<HTML>
<HEAD>
<TITLE>2726-11-TI-O3 Reset Cookie Home Page With Check</TITLE>
</HEAD>
<BODY>
<%
Dim varPeopleID, oRS
varPeopleID=Request.Cookies("PeopleID")
If varPeopleID = "" Then
Response.Write "<H1>This is the Sailor's Home Page, but...</H1>"
Response.Write "<P>(failed cookie check)</P>"
Response.Write "Sorry, we can not find your cookie<BR>"
Response.Write "<A href='2726-11-TI-O3-ResetCookieForm.asp'>"
Response.Write "Please click here </A>to re-set your cookie"
Else
Response.Write "<H1>Welcome to the Sailor's Home Page</H1>"
Response.Write "<P>(passed cookie check)</P>"
Set oRS = Server.CreateObject("ADODB.Recordset")
sqltxt = "SELECT PeopleNameLast, PeopleNameFirst, "
sqltxt = sqltxt & "PeopleProfessionalClass "
sqltxt = sqltxt & " FROM People WHERE PeopleID=" & varPeopleID & ";"
oRS.Open sqltxt, "DSN=Sailors"
Response.Write "<P>We have you, " & oRS("PeopleNameFirst") & " "
Response.Write oRS("PeopleNameLast") & ", "
Response.Write "registered in Professional Class "
Response.Write oRS("PeopleProfessionalClass") & "</P>"
End If
%>
</BODY></HTML>
The above cookie-checking page gives the same result as the last page if a cookie is present, but if the cookie is missing, then we get the following:

The user would click to reset a new cookie, and would hit the following code, file 2726-11-TI-03-ResetCookieForm.asp, to allow selection of the cookie to set:
<%@ Language=VBScript %>
<HTML><HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>2726-11-TI-O3 Reset Cookie Form</TITLE>
</HEAD><BODY>
<H1>Reset Cookie</H1>
<P>Sorry, but we are having problems identifying you.</P>
<P>Please select your name from the list below</P>
<FORM ACTION="2726-11-TI-O3-ResetCookieResponse.asp" METHOD=get>
<SELECT NAME="PeopleName">
<%
Dim oRSPeople
Set oRSPeople=Server.CreateObject("ADODB.Recordset")
sqltxt = "Select PeopleNameLast, PeopleNameFirst from People"
sqltxt = sqltxt & " ORDER BY PeopleNameLast;"
oRSPeople.Open sqltxt, "DSN=Sailors"
Do while not oRSPeople.EOF
Response.Write "<OPTION NAME=>"
Response.Write orsPeople("PeopleNameLast") & ", "
Response.Write orsPeople("PeopleNameFirst") & "</OPTION>"
oRSPeople.MoveNext
Loop
%>
</SELECT>
<INPUT TYPE="Submit">
<INPUT TYPE="Reset">
</FORM></BODY></HTML>
Which produces the following page:

The form needs a response, which in this case actually sets the cookie, and we would confirm that by informing the user of setting a cookie and giving her a hyperlink back to the home page, where she can use the new cookie. The code for this page is taken from 2726-11-TI-03-ResetCookieResponse.asp and is listed below:
<%@ Language=VBScript%>
<%Response.Buffer = true%>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>2726-11-TI-O3 Reset Cookie Response</TITLE>
</HEAD>
<BODY>
<P> </P>
<H1>This page will re-set your cookie </H1>
<%
varPeopleNameTotal = Request.Querystring("PeopleName")
varPeopleNameLast = left(varPeopleNameTotal,instr
(varPeopleNameTotal, ",")-1)
varPeopleNameFirst = right(varPeopleNameTotal,len
(varPeopleNameTotal)- instr(varPeopleNameTotal, ",")-1)
Dim oRSPeopleIDLookUp
Set oRSPeopleIDLookUp=Server.CreateObject
("ADODB.Recordset")
sqltxt = "Select PeopleID from People"
sqltxt = sqltxt & " WHERE PeopleNameLast = '" &
varpeoplenamelast & "'"
sqltxt = sqltxt & " AND PeopleNameFirst = '" &
varpeoplenamefirst & "';"
oRSPeopleIDLookUp.Open sqltxt, "DSN=Sailors"
Response.Cookies("PeopleID") = oRSPeopleIDLookUp
("PeopleID")
Response.Cookies("PeopleID").Expires = date + 365
%>
We have re-set your cookie, the identification is now for:<BR><BR>
<%=varPeopleNameFirst%> <%=varPeopleNameLast%>
with the ID number <%=oRSPeopleIDLookUp("PeopleID")%>
<BR><BR>Click here to re-try
<A HREF="2726-11-TI-O3-ResetCookieHomePageWithCheck.asp">
homepage</A>
with your new cookie
</BODY>
</HTML>
Which gives the user the following page, reporting back to the user that the cookie has been set.
This is from Beginning ASP Databases by Kauffman, Spencer, and Willis (Apress, ISBN 1590592492). Check it out at your favorite bookstore today.
Buy this book now. |
Next: How It Works – Resetting a Cookie >>
More ASP.NET Articles
More By Apress Publishing