Databases and Cookies - Try It Out – Setting a Cookie Using ASP–ADO
(Page 8 of 13 )
Our objective is to set a cookie with the user's ID number, then use that to look up the visitor's yacht club code in the database, and display that information on the home page. To make this example simpler, we will assume the visitors know their membership number from their membership card.
Firstly, let us look at the overall strategy that we will use. The four steps we will follow are:
- Get the visitor's membership number on a form.
- Second, we set a cookie that holds the visitor's ID number from the People table.
- Third we can use the ID from the cookie to find their record in the table.
- Lastly, from the correct record we can show the person's yacht club code.
This will require three pages. The first is a CookieSetterForm page that gathers the visitor's ID. The second page, CookieSetter, actually sets the cookie and confirms that act to the user. The third page, CookieHome, uses the emerging cookie and ADO to derive the name of the visitor's yacht club code.
The first page, in file 2726-11-Cook-TIO-02-CookiesWithADOCookieSetterForm.asp follows:
<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>2726-11-TI-O2 Cookies With ADO CookieSetterForm</TITLE>
</HEAD>
<BODY>
<H1>Welcome to the Sailors Site, But First...</H1>
<P>Please provide your membership number so we can set your cookie.</P>
<FORM ACTION="2726-11-Cook-TIO-02-CookiesWithADOCookieSetter.asp" METHOD=post>
Please type your membership number here
<INPUT NAME='PeopleID'>
<INPUT TYPE=submit VALUE="Submit">
</FORM>
</BODY>
</HTML>
Which gives this result:
The second page, 2726-11-Cook-TIO-02-CookiesWithADOCookieSetter.asp, takes the data and uses it to set a cookie, as follows:
<%@ Language=VBScript %>
<%Response.Buffer=true%>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>2726-11-TI-O2 Cookies With ADO CookieSetter</TITLE>
</HEAD>
<BODY>
<%
varPeopleID = Request.Form("PeopleID")
'next line for testing
'Response.Write varpeopleID
Response.Cookies("PeopleIDNumber")=varPeopleID
Response.Cookies("PeopleIDNumber").Expires = Date + 366
%>
A cookie has been set with your membership ID number of <%=varPeopleID%><BR>
<A HREF="2726-11-Cook-TIO-O2-CookiesWithADOCookieHome.asp">
Click here to return to the home page</A>
</BODY>
</HTML>
The above code gives the following result:
Then we finish by clicking back to the home page, whose code is in file 2726-11-Cook-TIO-02-CookiesWithADOCookieHome.asp and below:
<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>2726-11-TI-O2 Cookies With ADO Home</TITLE>
</HEAD>
<BODY>
<H1>Welcome to the Sailor's Home Site <BR><BR>
<%
Dim oRS, varPeopleIDNumber
varPeopleIDNumber = Request.Cookies("PeopleIDNumber")
Set oRS=Server.CreateObject("ADODB.Recordset")
sqltxt = "SELECT PeopleNameFirst, PeopleNameLast,
PeopleClubCode"
sqltxt = sqltxt & " FROM People "
sqltxt = sqltxt & " WHERE PeopleID = " &
varPeopleIDNumber & ";"
oRS.Open sqltxt, "DSN=sailors"
Response.Write oRS("PeopleNameFirst") & " "
Response.Write oRS("PeopleNameLast")
%>
</H1>
Your yacht club code is <%=oRS("PeopleClubCode")%>
</BODY>
</HTML>
The above code produces the following screen:
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 – Setting a Cookie Using ASP–ADO >>
More ASP.NET Articles
More By Apress Publishing