Databases and Cookies - How it Works – Setting and Reading a Cookie with ASP Alone
(Page 6 of 13 )
The first page is a simple HTML form. Remember that when using radio buttons on a form, one VALUE will be assigned to the NAME and then sent to the response page.
<FORM ACTION="2726-11-Cook-TIO-01-CookieSetter.asp" METHOD=get >
<INPUT TYPE="radio" NAME="PrefFontSize" VALUE=small> Small
<INPUT TYPE="radio" NAME="PrefFontSize" VALUE=medium CHECKED> Medium
<INPUT TYPE="radio" NAME="PrefFontSize" VALUE=large> Large<BR><BR>
<INPUT TYPE=submit>
</FORM>
</BODY></HTML>
The next page is the response page, as named in the ACTION attribute above. This page will be writing the cookie so we must be sure to buffer the page before starting the usual <HEAD> information:
<%@ Language=VBScript%>
<%Response.Buffer=true%>
<HTML><HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>2726 chapt11 Cookies TIO#01 Create Cookie that holds the Preferences</TITLE>
</HEAD><BODY>
Now we pick the data of the cookie out of the Request object and assign it to a variable. I like to report back to the user what she has selected.
<%
varPrefFontSize = Request.Querystring("PrefFontSize")
Response.Write "Your cookie holds a font size preference of " &_
varPrefFontSize & "<BR>"
Once safely ensconced in a variable, we can use the data as the text for a cookie. Since cookies by default expire when the browser closes, we need to explicitly set an expiration:
Response.Cookies("PrefFontSize")=varPrefFontSize
Response.Cookies("PrefFontSize").Expires = date+365
%>
Now that the cookie is set we can give the user a hyperlink to the home page to try out the new convenience.
<A HREF="2726-11-Cook-TIO-01-HomePageUsingSizePreference.asp">
Now that the font size is set, click here to go to the home page.</A>
The home page that uses the cookie has two steps. After the housekeeping, here is the game plan laid out.
<%@ Language=VBScript %>
<HTML><HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>2726 chapt11 Cookies TIO#01 HomePage Using the Font Size Preferences</TITLE>
</HEAD><BODY>
<%
'--------------------
' the plan to use the font size cookie preference on this page
' get cookie
' use sailor preference in Select Case to set styles
To pick up the cookie we query the Request object and use that result to fill a variable. You can see the artifact of development where I checked the cookie on the browser screen. I looked at the results while building the page, then at the time of deployment added an apostrophe to the beginning of the line which turned the command into a comment and thus prevented execution of the Response.Write. I like to leave these in the code because later, when there is another round of problems and troubleshooting, these tools are available by just deleting the leading apostrophe.
'--------------------
' get cookie
'
varPrefFontSize = Request.Cookies("PrefFontSize")
' next line for testing
'Response.Write "<HR>The cookie is " & varPrefFontSize & "<HR>"
Now we run a Select Case against the variable that contains the cookie contents. Note that we must write the <STYLE> and </STYLE>
tags regardless of the preference, so those are outside of the Select Case. Within the Select Case only one of the style definitions will be written.
'--------------------
' use sailor preference in Select Case to set styles
'
Response.Write "<STYLE>"
Select Case varPrefFontSize
case "large"
Response.Write "P {font-size: 26 pt}"
case "medium"
Response.Write "P {font-size: 16 pt}"
case "small"
Response.Write "P {font-size: 6 pt}"
End Select
Response.Write "</STYLE>"
%>
The remainder of the page contains simple HTML, which uses <p><p> styles that have been modified within the above code:
'--------------------
' use sailor preference in Select Case to set styles
'
Response.Write "<STYLE>"
Select Case varPrefFontSize
case "large"
Response.Write "P {font-size: 26 pt}"
case "medium"
Response.Write "P {font-size: 16 pt}"
case "small"
Response.Write "P {font-size: 6 pt}"
End Select
Response.Write "</STYLE>"
%>
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: Using Cookies with ADO and a Database >>
More ASP.NET Articles
More By Apress Publishing