Get great deals from the
Internet via E-mail CLICK
HERE
Member Newsletter
BUILD
YOUR SITE
Advanced:
Cookies--What, Why, How, and Where
Your
earliest memories of cookies may involve a blue monster
and a television set. Even after your Cookie Monster days
were over, you probably felt secure in your knowledge that
a cookie was a delicious morsel of sugar happiness. However,
the concept of a cookie has recently undergone yet another
transformation: cookies aren't just for eating anymore.
A cookie is now a vehicle that allows cyber relationships
to occur between Web sites and their visitors.
In the
most basic sense, cookies are small pieces of information
stored in your computer rather than in a database on a Web
server. This information may include your user name, which
pages you visited, and other basic information about your
visit. Cookies can be compared to the people at your favorite
restaurant or supermarket. Just as these people remember
details about you as you frequent the establishment, cookies
allow recognition in cyber space. Under the ideal scenario,
when you visit a site that wants to remember you, a cookie
is stored on your computer. The next time you visit the
Web site, it finds the cookie and "remembers"
you. The
Web site might then offer information that you haven't seen,
or cater the text to use your name.
It is
important to point out that cookies get information about
you from ....you. A Web site can usually get information
about your operating system and Web browser, but these details
do not expose your personal information. If a Web site wants
to remember your name, it has to first ask you for your
name. Therefore, you can still control the level of information
that you choose to reveal about yourself.Cookies are created
with information you provide on a form or with arbitrary
information, such as a number which is set to 1 when the
cookie is created and then incremented each time the page
is visited and the cookie is read. Cookies can also designed
to be read only by the Web site that created them. This
means that if you visit an unscrupulous Web site that wants
information from other Web sites you have visited, it cannot
access the cookies created by other Web sites. Some are
concerned about security holes in the way various browsers
handle cookies. For more information click
here. Cookies
can also contain a built-in timer which sets a date on which
the cookie will "die" (or be removed from the
cookie cache by the Web browser). In summary, the cookie
mechanism stores "state" data in a more permanent
(or "persistent") form.
Why
are they used?
Cookies
can be used toward any end in which remembering information
from one page or visit to the next is important. Cookies
are commonly used to allow different information to occupy
a page based on your preferences. For example, if you visit
your favorite portal site, you may notice that the page
displays your stocks, some news, and the weather. Because
this information is displayed without your logging in, your
browser has a cookie set indicating your locale and your
preferences for which information you want to see. Cookies
are also commonly used to track how often you return to
a Web site, particularly because the success of many companies
and advertising effort are measured by repeat visitors.
Each time you visit a Web site that is gathering this information,
a value in the cookie is incremented, and the updated information
is captured by a Web server, which uses it to extrapolate
reports based on the data.
How
do I use them?
If you
are creating a Web page that would benefit from storing
user information in anticipation of repeat visits, then
you may want to use a cookie. The life cycle of a cookie
can be illustrated in basic steps.
A cookie can be requested in the following ways: First,
your Web page may request a cookie when a visitor types
the page's URL or clicks a link to it. Second, if the
server storing the Web page implements the cookie, then
a CGI or the server requests the cookie data from the
client machine before passing the Web page. Third, if
the cookie is implemented by a visitor's computer with
the Web browser, then, while loading the Web page, the
browser executes some script that checks for the existence
of your cookie.
If a cookie exists, then the browser reads and stores
it according to your script instructions. The script must
also contain code indicating how to split the cookie into
the individual name and value pairs that
comprise it and then make decisions based on the data.
Finally, if necessary, the values may need to be updated
to reflect the outcome of this visit and then be stored
in the cookie again in anticipation of the next visit
by this visitor.
Or, if a cookie does not exist, the script must instruct
the browser how to create one using the settings associated
with a visitor's first visit.
Here
is a very basic example illustrating how to use JavaScript
to create and read a cookie. This cookie will maintain a
counter which will be incremented by one each time it is
read. Because we don't want to clutter up our visitor's
cache, we will treat visitors that don't return within one
week new visitors if they return later. We will therefore
set a one-week expiration date for our cookie.
<SCRIPT LANGUAGE="JavaScript"><!--
expireDate=new Date(); //create variable to store the current date.
expireDate.setDate(expireDate.getDate()+7); //set the value to seven days from now
hits=eval(cookieVal("pagevisits")); //call function to split off pagevisits name and its value
hits++; //increment the value
document.cookie="pagevisits="+hits+";expires="+expireDate.toGMTString()
//store the new value and the expiration date back into the cookie (or for the first time)
function cookieVal(cookieName){ //function breaks the cookie into name and value pairs
thisCookie=document.cookie.split("; "); //semi-colon is the delimiter seperating fields
for(i=0; i<thisCookie.length; i++){ //loop through the entire length of the cookie string
if(cookieName==thisCookie[i].split("=")[0]){ //check for the value we passed in the function call
return thisCookie[i].split("=")[1]; //if the value matches then send back what follows the = sign
}
}
return 0;
}
if (hits>2){
document.write("Welcome back! You've visited this page "+hits+" times.");
//do something the visitor can see
}
//--></SCRIPT>
The
information in the cookie is saved as text. Here are the
contents of the cookie, which is created when this page
is rendered by Internet Explorer 5.0. Notice the pagevisits
variable name (the name part of the name value pair)
and the value part of the pair, which is 25 (representing
the number of times I reloaded this page to verify that
the cookie was working). Below that is the location of the
Web site which created the cookie (this is how cookies belonging
to one Web site are protected from other Web sites; the
location for each site is unique). Then you'll see the expiration
date in Greenwich Mean Time, encoded. This information is
used by the Web browser so it knows when to discard the
cookie.
The
Web browser that you use determines where and how cookies
are stored. Each browser handles them a little differently.
Here are a list of common Browser/Platform combinations
which indicate where the cookies are stored.
If you are using Windows and Internet Explorer 3.x,
each cookie is stored as a separate file in the folder
C:\WINDOWS\COOKIES.
If you are using Windows and Internet Explorer 4.x, or
5.x, each cookie is a separate file stored in C:\WINDOWS\TEMPORARY
INTERNET FILES.
If you are using a Macintosh with Internet Explorer 2.x,
cookies are stored together in a file name COOKIES.TXT
in the SYSTEM FOLDER:PREFERENCES:EXPLORER folder.
If you are using a Macintosh with Internet Explorer 3.x,
the cookies are stored in SYSTEM FOLDER:PREFERENCES:INTERNET
PREFERENCES.
If you are using a Macintosh with Internet Explorer 4.x,
then the cookies are stored in SYSTEM FOLDER:MS:PREFERENCES
PANEL:COOKIES FILE.
If
you are using Windows and Navigator 4.x, each cookie is
stored in a single file name COOKIES.TXT in the directory
where Navigator was installed (the default is C:\PROGRAM
FILES\NETSCAPE\USERS\DEFAULT).
Under many of these combinations, it is actually possible
to turn cookies off or to have a message come up each time
a Web site attempts to write a cookie, asking you if you
want to allow it. Frankly, the latter becomes a serious
hindrance to enjoying surfing the Web because so many Web
sites use the cookie mechanism that you will answer the
question many, many times ad nauseam. The former option,
turning cookies off, breaks enough legitimate functionalities
that return visits to Web sites you frequent become a nuisance.
For example, If a cookie has been used to store your login
information and you choose to disable it, then you must
enter your information each time you visit the page instead
of automatically being logged in. Another common problem
with disabling cookies is advertising; often a cookie keeps
track of whether you have already been shown an ad. If you
have been shown the ad, then it won't be displayed again,
but if you have cookies turned off, then each page which
checks for the cookie may display the ad over and over.
In the final analysis, cookies can be very helpful for a
Web visitor, so it may not be desirable to disable them.
Periodically cleaning the cookies up by deleting the files
which store them or simply removing individual cookies is
the best means of managing them. To that end, a number of
programs aid anyone concerned with the contents of cookies
stored by their Web browser.