Cookies are one of the most widely used, misused, and misunderstood features of the Web. Their basic function is simple: allow Web servers to store and retrieve information on the client-side. Although cookies can make the Web surfing experience more personalized and streamlined, many users regard them with suspicion because of concerns about privacy.
When used appropriately, cookies can be an invaluable tool for a webmaster. You can use them to simplify sign-on procedures, set up shopping carts, and provide individual users with more personalized information on your site. Site visitors are becoming increasingly discriminating: they demand useful content presented quickly and clearly.
Cookies can help you incorporate those features into your site.
What Cookies Are – And Are NOT
First, you must understand what cookies are:
|
Even more important, understand what cookies are NOT:
|
A good example of a simple cookie is on the Amazon.com Web site. Registered users see a personalized message when they visit:
“Hello, John Smith, we have recommendations for you in books, music, and video.”
Links take the user to a personalized page that highlights products based on their personal profile.
Useful Applications For Cookies
Web sites use cookies for a number of different reasons. Some of the most common include:
Site Personalization – As in the Amazon example above, you can use cookies to identify visitors and direct them to areas of you site that might interest them most.
This can be as simple as flagging new stories or products added to the site since their last visit or as complicated as rendering pages that are almost completely customized based on stored preferences (favorite music, nonfiction categories, etc).
Online Ordering – Many e-commerce sites use cookies to track additions or deletions to your shopping cart. Sites can use session cookies that are valid only for the duration of that particular visit, or design them so that the user can return to the site days later and complete their transaction.
Web site Tracking – Cookies provide you with a more accurate count of site visitors. Using cookies, you can insure that someone who visits your site 3 times per day isn’t counted as a unique user each time. You can also see how often repeat visitors visit and what items they view most often (very helpful if you want to personalize a site).
You can use this script as the basis for more complex cookie operations using JavaScript, such as handling multiple cookies and more custom pages that are even more targeted. You are limited only by your imagination and the willingness of your site visitors to share information.
When used appropriately, cookies can be an invaluable tool for a webmaster. You can use them to simplify sign-on procedures, set up shopping carts, and provide individual users with more personalized information on your site. Site visitors are becoming increasingly discriminating: they demand useful content presented quickly and clearly.
Cookies can help you incorporate those features into your site.
What Cookies Are – And Are NOT
First, you must understand what cookies are:
|
Even more important, understand what cookies are NOT:
|
A good example of a simple cookie is on the Amazon.com Web site. Registered users see a personalized message when they visit:
“Hello, John Smith, we have recommendations for you in books, music, and video.”
Links take the user to a personalized page that highlights products based on their personal profile.
Useful Applications For Cookies
Web sites use cookies for a number of different reasons. Some of the most common include:
Site Personalization – As in the Amazon example above, you can use cookies to identify visitors and direct them to areas of you site that might interest them most.
This can be as simple as flagging new stories or products added to the site since their last visit or as complicated as rendering pages that are almost completely customized based on stored preferences (favorite music, nonfiction categories, etc).
Online Ordering – Many e-commerce sites use cookies to track additions or deletions to your shopping cart. Sites can use session cookies that are valid only for the duration of that particular visit, or design them so that the user can return to the site days later and complete their transaction.
Web site Tracking – Cookies provide you with a more accurate count of site visitors. Using cookies, you can insure that someone who visits your site 3 times per day isn’t counted as a unique user each time. You can also see how often repeat visitors visit and what items they view most often (very helpful if you want to personalize a site).
Sample Cookie Script Using JavaScript
Working with cookies can be complicated. Don’t expect that you’ll be able to implement a sophisticated Web site tracking system as soon as you start experimenting with cookies. To make full use of cookies requires extensive knowledge of CGI and database programming. However, adding a basic cookie script to your site is easy, and can be done using some simple JavaScript code.
This example shows you how use JavaScript to set a cookie that will accept and store a user name, then retrieve the stored information to display on your Web page.
Remember, cookies involve a two-step process: first you set the cookie, and then you retrieve it.
- Setting a Cookie
- Place this script inside the HEAD tag of your page.
- Insert this inside the BODY tag of your document.
- Place this inside the BODY section wherever you want the input box to display.
Now when a visitor comes to your page, they’ll see a form asking them to select a user name for themselves. When they click the “Go” button, our JavaScript will issue a cookie that stores this user name. From then on, when this visitor comes to our site, we can recognize them by this name.
Click here to see a sample page that uses this script to set a cookie.
- Reading the Cookie You Just Set
Naturally, our cookie is useless if you don’t have a way to access it. To do that, you can insert the JavaScript below into any page in your site. Place it wherever you would like the user name to appear.
If your visitor has selected a user name and been issued a cookie, this script will print the message “Welcome back (User Name).” If they haven’t received your cookie, it will print “You haven’t registered with our site.”
Click here to see this script working on a page.
You can use this script as the basis for more complex cookie operations using JavaScript, such as handling multiple cookies and more custom pages that are even more targeted. You are limited only by your imagination and the willingness of your site visitors to share information.