Cookies - A Tutorial by Kyle McIntyre Valid XHTML 1.0!

First time here? Welcome to the site! A log of this visit has been recorded using cookies. Refresh the page to see cookies in action!
What are Cookies? | Why are Cookies Useful? | Examples | Cookies And Sessions | Security Concerns

Why are Cookies Useful?

Cookies are a powerful tool because they allow web developers to easily perform long-term user recognition. Although session IDs and GET/POST variables allow a web site to continually adapt to a user as he or she navigates a web site, only cookies can persevere if the user decides to reboot or navigate away from the page. In this case, cookies can be used to identify the user the next time he or she visits the site and the proper settings can be reapplied. Cookies are one of the simplest ways to provide users with a tailored web experience. However, the choice to incorporate cookies into your website does not imply the exclusion of database and server session technologies. Instead, these technologies complement each other nicely as we will see in the following examples.

Let's think of a few instances where cookies are already in use across the web. One widespread use of cookies is the ability of a web site such as Hotmail to retain its users' login information. If a user elects to have his or her login information remembered, then the appropriate login and (encrypted) password are stored on the user's local machine in the form of cookies. When the user returns to Hotmail, his or her cookies are passed back to the server, decrypted, and used to facilitate a user login that eventually allows Hotmail to extract the user's settings and emails from a relational database.

Other uses of cookies include website statistics, ecommerce shopping carts, and tailored news and weather applications. In the case of tailored news and weather applications, a cookie is often used to store the user's zip code so that the appropriate stories and forecasts can be relayed to the user, who never has to login or create an account! The great thing about cookies is that they are well supported in nearly every scripting language. Such languages include PHP, Javascript, JSP, ASP, Python, and Cold Fusion. The following code examples are implemented with ease in JSP, Javascript and PHP. They can be considered representative of general cookie usage.

Examples

Email: mcintyr@cs.montana.edu   Last Updated: June 8th, 2005