Introduction
The goal of every aspiring business, whether it is a local grocery shop or a big company,
is to know and recognize it's clients. Whenever you go into a local shop, the salesperson
jumps at you and greets you. He knows you, he knows what your preferences are and he'll point
you the sales most suited for you.
Your commercial web site is no different - you should be able to recognize your
clients on the spot and lure them to what interests them most.
But alas! Your homepage is nearsighted. Or at least that's what you thought!
In this article you are going to learn the mastery of bakery - a recipe for Cookies.
What are those Cookies then?
Cookies are small text files, which are sent from your server to the client's hard drive.
Cookies are used to store information about the client, so the next times he comes we'll be able to
recognize him and his interests. Suppose you're running a big portal that covers lots of different issues
from politics to ninja turtles. You retrieve the Cookie from the client's computer and customize
your site on the spot for the client's needs, so he'll see the recent interview with Splinter right on the homepage!
Get it? Good.
Now lets get to it!
Writing a Cookie
So, how do we actually write a Cookie? Simple!
We use the Response
object like this: Response.Cookies("Cookie") = Value
For example:
Response.Cookies("UserName") = "James Bond"
Now, suppose we want to store more than one field: simple - we use an Indexed Cookie.
That's how it's done: Response.Cookies("Cookie")("Key") = Value
Response.Cookies("User_Info")("fname") = "James"
Response.Cookies("User_Info")("lname") = "Bond"
Response.Cookies("User_Info")("id") = "007"
If the Cookie is not existent, it will automatically be created.
If it is already existent, this code will overwrite it.
Warning: the code for writing a Cookie should appear before any HTML tag.
That's about it for writing a Cookie, but how can we read it?
Reading a Cookie
As you might have guessed, reading a Cookie is very similar to writing one. The only difference - we
use the Request
object.
For a simple Cookie: var = Request.Cookies("Cookie")
UserName = Request.Cookies("UserName")
The Indexed Cookie is no different of course: var = Request.Cookies("Cookie")("Key")
For example:
fname = Request.Cookies("User_Info")("fname")
lname = Request.Cookies("User_Info")("lname")
id = Request.Cookies("User_Info")("id")
Response.Write fname & lname & " logged in."
Carry on!
Expiration Date
To prevent the client from keeping outdated Cookies, every Cookie has an expiration date. When this
date exceeds, the Cookie is deleted. If we don't set an expiration date for a Cookie it is
deleted when the client leaves our site.
That's how we set the expiration date: Response.Cookies("Cookie").Expires = date
Response.Cookies("User_Info").Expires = "01/01/2003"
Now, this code is not very useful because we'll have to update it all the time.
Here's a more efficient example:
Response.Cookies("User_Info").Expires = Now() + 100
// it sets the expiration date 100 days from the current date
Sometimes we want to delete an existing Cookie. There's no direct way of doing so, but we can go
around it by setting the expiration date to an old date. Check it out:
Response.Cookies("User_Info").Expires = "01/01/1980"
Ok, we're almost there!
Cookie Path
Another thing you should know about is the Cookie path.
Whenever you write a cookie, it saves the path of the site that generated it.
When the user revisits that site the Cookie is automatically sent to the server located in that path.
You can change the default path of the Cookie to point to another path.
Like this: Response.Cookies("User_Info").Path = NewPath
Suppose you run two applications and you want both to correspond to the same Cookies. Simply
write a Cookie and point it to your other application as well:
Response.Cookies("User_Info").Path = "http://MyServer/MySecondApp/"
Well that's the recipe!
Summary
In this article we've gone through the basic usage of Cookies. However, I merely gave you the tool, the
recipe - it's up to you to put it in good use!
You should know that not all browsers support Cookies, and some can disable it in their options.
So don't rely too much about them, try figuring a way around them or check if the user's browser supports
them - if it doesn't, notify the user!
And remember, if 50 cooks use the same recipe you still get 50 different tastes, so find your own
way to implement this one.