Working With Cookies in ASP
Cookies are small text files that are stored on users' machines after they visit your website. When they go back to a website, the cookie information is sent along also, and can therefore be retrieved by the site that set them. Cookies are a great way to store non-essential user information.
For essential information, such as logins/passwords etc. a database or similar server-side solution should be used. The main reason for this is that cookies are stored locally on the users' machine, meaning that as a designer, you have no real control over them once you set them. If users delete their cookie, the information they contain is lost. Also, some users have their browsers set to not accept cookies, so placing too much dependence on them could lead to a loss of functionality for those users.
With that said, for other uses, such as storing user preferences (website themes, favorite category, etc.) cookies are a great solution, since they don't require any server-side storage means, which can be very useful for busy sites. Here we'll look at setting cookies and also retrieving their contents.
Setting/Retrieving Cookies
For this example, we're going to take on a pretty complicated example, that will hopefully show several applications of cookies. We're going to keep track of the user's first and last name, and also keep track of how many times this user has visited a particular page. Let's say that this user's name was John Smith (we could have gathered this from an input form for example).
We'll set three different cookies: firstname, lastname, and a special expiry date for the third cookie, numvisits, using the following:
<% Response.Cookies("firstname")="John" Response.Cookies("lastname")="Smith" Response.Cookies("numvisits").Expires=date+365 %>
What we've done here is defined 2 cookie values, one each for both the first and last name. We've also set the third cookie, numvisits, to expire 365 days from now. If you wanted to know how many times the user had visited today, you might set this to date+1, for a week, date+7 etc.
We usually want to do something with the information we store in a cookie, and for starters, we can put that data into some holding variables, which we can work with later. We use the following:
<% dim sFirstName, sLastName, iNumVisits sFirstName = Request.Cookies("firstname") sLastName = Request.Cookies("lastname") iNumVisits = Request.Cookies("numvisits") %>
Next we need to set up some code to increment the numvisits counter. One time for each time they visit the page. If it's their first time here, we'll need to set it equal to 1. Finally, we want to do something with the cookie's information, for example, greet the user and let them know how many times they've been here. Here's the finished code:
<% dim sFirstName, sLastName, iNumVisits sFirstName = Request.Cookies("firstname") sLastName = Request.Cookies("lastname") iNumVisits=request.cookies("numvisits") if iNumVisits="" then Response.Cookies("numvisits")=1 Response.Write("Welcome this is your first visit.") else Response.Cookies("numvisits")=iNumVisits+1 Response.Write("Welcome back " & sFirstName & sLastName & "!") Response.Write("You've been here " & iNumVisits & " time(s)") end if %>
Based on whether or not they've been here, the user sees a different message. This is accomplished using a basic if..then statement.
Conclusion
We've barely scratched the surface of what cookies can do. Now that you're armed with the basics of setting and retrieving cookie information, experiment with their usage. For example, you could set a cookie that would remember a user's last page they visited, and if they return, you could automatically take them there, or print out a reminder saying "do you want to continue where you left off?" etc. There's lots of neat uses for cookies, and they're a great way to add a sense of interactivity for the users. As I cautioned earlier though, don't be too dependent on cookies for really important information, since you can't be sure that data will be saved, or even be available (if the user has cookies turned off).
- 314 reads

