ASP Forms - Cookies
(Page 2 of 5 )
Cookies are truly the breakfast of champions. If you've ever watched an episode of Pig Olympics, you know what I am talking about. And if you haven't, head over to YouTube and give it a search. And while you are at it, check out a weightlifter by the name of Ronnie Coleman. Guaranteed in no time you will be shouting YEAP and LIGHTWEIGHT at the top of your lungs.
Aside from being delicious and highly nutritious, Cookies are small files that the villainous server places on the user's computer. Take that user! Whenever the user logs back onto the web page, it sends that cookie back (who wants a used cookie?). This allows us to "remember" certain aspects about the user.
Here is how you create a cookie in ASP:
<%
Response.Cookies("name")="Monster"
%>
This stores the name "Monster" in the name cookie. We can also determine how long the cookie will stay on the user's computer, by assigning an expiration property to it:
<%
Response.Cookies("name")="Monster"
Response.Cookies("name").Expires=#April 22, 2008#
%>
This code creates the cookie and stores the value in it, then gives the cookie an expiration date.
Getting a Cookie from the Cookie Jar
It's one thing to create a cookie, but another to retrieve that cookie without getting caught by our parents at four in the morning. I mean come on: I'm thirty. Can't I have a cookie when I want one?
We use the Request.Cookies command to jack those cookies. Here is how we retrieve a cookie and then display it for all the world to see:
<%
username=Request.Cookies("name")
response.write("Leave my cookies alone you " & username)
%>
This would retrieve the cookie from the users computer and display the following to the screen:
Leave my cookies alone you Monster
Next: A Baker's Dozen >>
More ASP Code Articles
More By James Payne