Classic ASP Cookies

Today I want to review the uses of classic ASP cookies and how to use them. Let us say that we want to save the last item a customer viewed for the next time they return. This is accomplished really easily, let us take a look.

Create the cookie on different item pages:

<%
‘On the item page at the bottom we want to create the cookie
Response.Cookies(”ItemNumber”) = 123

‘We want to keep the cookie for about 7 days
Response.Cookies(”ItemNumber”).Expires = Date() + 7
%>

To call the Cookie all you have to do is:

<%
Dim LastItem
LastItem = Request.Cookies(”ItemNumber”)
Response.Write LastItem
%>

Now here is something that you might find very useful, what if you wanted to pass this cookie along to multiple sub-domains? To do this we need to make some changes to the beginning code.

<%
‘On the item page at the bottom we want to create the cookie
Response.Cookies(”ItemNumber”) = 123

‘We want to keep the cookie for about 7 days
Response.Cookies(”ItemNumber”).Expires = Date() + 7

‘Pass to all sub-domains
Response.Cookies(”ItemNumber “).Domain = “.myserver.com”
%>

And that’s it.

Comments are closed.