Cookie Power Made Easy
By Stephen Bucaro
Cookies are a way of storing information about a user on the users computer. Cookies
can be used to track a users visits to and path through a web site, store a users
choices, and much more. A cookie can not be larger than 4096 bytes, only a maximum
of 20 cookies can be stored per domain, and only 300 cookies total can be stored.
Where cookies are stored on the users computer varies with the browser version and
the operating system. In some cases all cookies are written to a single file. On
Windows 2000 cookies are stored under each users My Documents folder. On Windows 98
cookies are stored under the Windows folder.
All the cookies saved from an individual web page are stored in one file. Each web
page has a related cookie file. When a user requests a web page from a server, the
browser searches for a related cookie file on the users computer. The cookie file
would have been created by previous visits to that web page.
If any cookies are found they are loaded into the page's document.cookie property.
If you are familiar with the browser document object model, you would expect the
cookie property to contain a collection of cookies. Instead, the cookie property
contains a single string of text.
It is easy to store a cookie from your web page. Just use JavaScript to assign
values to the document.cookie property as shown below.
document.cookie="cookiename=cookievalue";
The line above shows the minimum requirement to create a cookie. A cookie may
contain up to five parts separated by semicolons as shown below.
"name=value; expires=date; path=path; domain=domain; secure"
Because all the cookies related to one web page or one domain are written to a single
file, retrieving a cookie is a bit more difficult. For experimenting or debugging
you can generate a popup dialog box displaying a list of all the cookies related to
a web page by typing the following line into the address text box of your browser.
JavaScript:alert(document.cookie.split(';').join('\n')
To use cookies with your web page you have to be able to extract the specific cookie
that you are interested in from the browsers cookie property string. The JavaScript
code below will perform that function.
function getCookie(name)
{
var cookieFound = false;
var start = 0;
var end = 0;
var cookieString = document.cookie;
var i = 0;
while(i <= cookieString.length)
{
start = i;
end = start + name.length;
if(cookieString.substring(start,end) == name)
{
cookieFound = true;
break;
}
i++;
}
if(cookieFound)
{
start = end + 1;
end = document.cookie.indexOf(";",start);
if(end < start)
end = document.cookie.length;
return document.cookie.substring(start,end);
}
return "";
}
frequently we want to store more than a single piece of information for a web page.
You could achieve this by setting and retrieving multiple cookies. However, if you
have other cookie setting code on your web site, for example advertising banners,
you could quickly exceed the 20 cookies per domain maximum. When you exceed the
maximum, the browser will delete the oldest cookie and retain the most recent 20.
|