Home > Articles

Creating and Using Cookies

Paul McFedries
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Close WindowPaul McFedries

Paul McFedries

Learn more…

How Paul McFedries Gets Under the Hood of Microsoft Products
Dec 10, 2009
Tweaking the Windows 7 Registry
Jul 17, 2009
Running Scripts to Enhance and Improve Windows
Jun 11, 2009
Building a Budget PC
Jun 16, 2008
Accessing and Sharing Network Resources in Windows Vista
May 28, 2008
Programming the Windows Script Host
Apr 29, 2008
Tricks for Working with Word 2007 Documents
Mar 31, 2008
Building Basic Formulas in Excel 2007
Mar 26, 2008
Collaborating with Windows Meeting Space
Feb 15, 2008
Troubleshooting Tools Included with Windows Vista
Feb 15, 2008
Using Virtual Private Network Connections in Windows Vista
Feb 15, 2008
Implementing Group Policies with Windows Vista
Feb 8, 2008
Programming the Windows Script Host
Feb 8, 2008
Windows Vista's Stability Improvements
Feb 8, 2008
Adding Devices to the Windows Home Server Network
Sep 21, 2007
Understanding Program Variables in VBA
Jun 15, 2007
Designing Microsoft Access 2007 Forms for Efficient and Accurate Data Entry
May 4, 2007
Getting the Most Out of Windows Vista User Accounts
Mar 9, 2007
The Windows Vista Interface
Oct 6, 2006
Mastering File Types in Windows XP
Nov 4, 2005
Constructing Knockout Presentations in PowerPoint
Apr 22, 2005
Working with Multiple-Table Queries in Access
Sep 10, 2004
Building Basic Formulas in Excel
Aug 27, 2004
Understanding Program Variables in VBA
Apr 2, 2004
From Buck-Naked to Beautiful: Dressing Up Your Page
Jun 7, 2002
Performing Financial Calculations
Sep 2, 2001
Creating and Using Cookies
Sep 2, 2001
Encrypting Text with JavaScript
Sep 2, 2001
Making Form Fields Mandatory
Sep 2, 2001
Cross-Browser Solutions: Detection and Generic Functions
Sep 2, 2001
Understanding Variables in JavaScript
Jul 6, 2001
Formulas and Functions: Microsoft Excel 2010, Rough CutsFormulas and Functions: Microsoft Excel 2010, Rough Cuts
Feb 2, 2010
Microsoft Windows Home Server Unleashed, Rough Cuts, 2nd EditionMicrosoft Windows Home Server Unleashed, Rough Cuts, 2nd Edition
Jan 6, 2010
Mac OS X Snow Leopard In DepthMac OS X Snow Leopard In Depth
Sep 22, 2009
Mac OS X Snow Leopard In Depth App (iPhone)Mac OS X Snow Leopard In Depth App (iPhone)
Sep 22, 2009
Mac OS X Snow Leopard In Depth, Adobe ReaderMac OS X Snow Leopard In Depth, Adobe Reader
Sep 15, 2009
Microsoft Windows 7 UnleashedMicrosoft Windows 7 Unleashed
Jul 14, 2009
Microsoft Windows 7 Unleashed, Adobe ReaderMicrosoft Windows 7 Unleashed, Adobe Reader
Jul 8, 2009
Tweak It and Freak It: A Killer Guide to Making Windows Run Your WayTweak It and Freak It: A Killer Guide to Making Windows Run Your Way
Mar 27, 2009
Tweak It and Freak It: A Killer Guide to Making Windows Run Your Way, Adobe ReaderTweak It and Freak It: A Killer Guide to Making Windows Run Your Way, Adobe Reader
Mar 18, 2009
Build It. Fix It. Own It: A Beginner's Guide to Building and Upgrading a PCBuild It. Fix It. Own It: A Beginner's Guide to Building and Upgrading a PC
May 19, 2008
Build It. Fix It. Own It: A Beginner's Guide to Building and Upgrading a PC, Adobe ReaderBuild It. Fix It. Own It: A Beginner's Guide to Building and Upgrading a PC, Adobe Reader
May 7, 2008
Microsoft Windows Vista Unleashed, 2nd EditionMicrosoft Windows Vista Unleashed, 2nd Edition
Apr 4, 2008
Microsoft Windows Vista Unleashed, Adobe Reader, 2nd EditionMicrosoft Windows Vista Unleashed, Adobe Reader, 2nd Edition
Apr 4, 2008
Networking with Microsoft Windows Vista: Your Guide to Easy and Secure Windows Vista NetworkingNetworking with Microsoft Windows Vista: Your Guide to Easy and Secure Windows Vista Networking
Dec 19, 2007
Networking with Microsoft Windows Vista: Your Guide to Easy and Secure Windows Vista Networking (Adobe Reader)Networking with Microsoft Windows Vista: Your Guide to Easy and Secure Windows Vista Networking (Adobe Reader)
Dec 19, 2007
Microsoft Windows Home Server UnleashedMicrosoft Windows Home Server Unleashed
Sep 13, 2007
Microsoft Windows Home Server Unleashed (Adobe Reader)Microsoft Windows Home Server Unleashed (Adobe Reader)
Sep 13, 2007
Tricks of the Microsoft Office 2007 GurusTricks of the Microsoft Office 2007 Gurus
May 16, 2007
Tricks of the Microsoft Office 2007 Gurus (Adobe Reader), 2nd EditionTricks of the Microsoft Office 2007 Gurus (Adobe Reader), 2nd Edition
May 16, 2007
Microsoft Office Access 2007 Forms, Reports, and QueriesMicrosoft Office Access 2007 Forms, Reports, and Queries
May 1, 2007
Microsoft Office Access 2007 Forms, Reports, and Queries (Adobe Reader)Microsoft Office Access 2007 Forms, Reports, and Queries (Adobe Reader)
May 1, 2007
VBA for the 2007 Microsoft Office SystemVBA for the 2007 Microsoft Office System
Mar 30, 2007
VBA for the 2007 Microsoft Office System (Adobe Reader)VBA for the 2007 Microsoft Office System (Adobe Reader)
Mar 30, 2007
Formulas and Functions with Microsoft Office Excel 2007Formulas and Functions with Microsoft Office Excel 2007
Mar 14, 2007
Formulas and Functions with Microsoft Office Excel 2007 (Adobe Reader)Formulas and Functions with Microsoft Office Excel 2007 (Adobe Reader)
Mar 14, 2007
Microsoft Windows Vista Unleashed, Adobe ReaderMicrosoft Windows Vista Unleashed, Adobe Reader
Dec 8, 2006
Microsoft Windows Vista UnveiledMicrosoft Windows Vista Unveiled
Jun 9, 2006
Microsoft Windows Vista Unveiled, Adobe ReaderMicrosoft Windows Vista Unveiled, Adobe Reader
Jun 9, 2006
Special Edition Using JavaScript, Adobe ReaderSpecial Edition Using JavaScript, Adobe Reader
Feb 14, 2006
Microsoft Windows XP UnleashedMicrosoft Windows XP Unleashed
Sep 6, 2005
Microsoft Windows XP Unleashed, Adobe ReaderMicrosoft Windows XP Unleashed, Adobe Reader
Sep 6, 2005
Tricks of the Microsoft Office GurusTricks of the Microsoft Office Gurus
Apr 12, 2005
Microsoft Access 2003 Forms, Reports, and QueriesMicrosoft Access 2003 Forms, Reports, and Queries
Aug 18, 2004
Microsoft Access 2003 Forms, Reports, and Queries, Adobe ReaderMicrosoft Access 2003 Forms, Reports, and Queries, Adobe Reader
Aug 18, 2004
Formulas and Functions with Microsoft Excel 2003Formulas and Functions with Microsoft Excel 2003
Jun 11, 2004
Formulas and Functions with Microsoft Excel 2003, Adobe ReaderFormulas and Functions with Microsoft Excel 2003, Adobe Reader
Jun 11, 2004
Absolute Beginner's Guide to VBAAbsolute Beginner's Guide to VBA
Mar 9, 2004
Absolute Beginner's Guide to VBA, Adobe ReaderAbsolute Beginner's Guide to VBA, Adobe Reader
Mar 9, 2004
Unauthorized Guide to Windows Millennium, TheUnauthorized Guide to Windows Millennium, The
Sep 20, 2000
The Unauthorized Guide to Windows 98, 2nd EditionThe Unauthorized Guide to Windows 98, 2nd Edition
Aug 28, 1999

Sorry, this author hasn't posted any blogs.

To store data such as user preferences, ordering information, and passwords, you need to create cookies. Paul McFedries guides you through the process in this article.
This article is excerpted from Special Edition Using JavaScript.

What Is a Cookie?

You've seen the benefits of global variables in many places so far in this book:

  • Within a single page, a global variable saves coding by making the variable's value available to all the statements, functions, and scripts on that page.

  • Within a frameset, global variables in the frameset page enable you to "store" values that are accessible to scripts within not only the frameset page, but also within all of the child frames.

Unfortunately, when the user navigates to a different page or frameset on your site, or when the user leaves your site and returns later, the values stored in all those global variables are lost forever. If you want to store data such as user preferences, ordering information, or user names and passwords, you need some way of creating a truly "global" variable that maintains its value across pages and across browser sessions.

The solution is to create something called a cookie. This is (depending on the browser) either a text file or a line within a designated text file. Either way, the file is saved on the user's computer, which means that the data in the cookie is available to any script on any of your pages, even if the user has shut down her system and rebooted. In JavaScript parlance, this is known as saving state.

In the simplest terms, a cookie is just a chunk of text that is stored on the user's hard disk. Where the cookie gets stored depends on the browser and on the operating system:

  • The Windows 9x versions of Internet Explorer store each cookie is a separate text file in the Windows\Cookies folder. In Windows 2000 and XP, the file is in the Documents and Settings\UserName\COOKIES folder (where UserName is the current user's Windows login name). The file usually takes the name UserName@Domain.txt (where Domain is the domain name of the site that saved the cookie). On the Macintosh, Internet Explorer stores the cookies in a file named cookies in the SystemFolder:MS Preference Panels folder.

  • Netscape stores each cookie as an entry in a single text file. In Windows, the file is named cookies.txt and is located in the Netscape\Users\UserName\ folder. For the Macintosh, the file is named MagicCookie and can be found in the System Folder:Preferences:Netscape Users:UserName folder. (In both cases, UserName is the current user's Netscape profile name.)

The most basic form of cookie acts similar to but is a bit different than a JavaScript variable. It consists of a name/value pair, which means that it has a name and value separated by an equals sign (=). For example, if you want to use a cookie to store a person's user name, you might use the name user_name and the value Biff. In this case, the simple cookie would look like this:

user_name=Biff

This looks a lot like a variable assignment statement in JavaScript, which is why cookies and variables are similar. However, you need to remember that the cookie is the entire name=value statement, not just the name or the value.

This simple cookie will serve you well for many uses, but, as you'll see a bit later when I show you how to set a cookie, you can also create more advanced cookies that have expiration dates and other useful features.

The other important cookie crumb that you need to bear in mind is that all cookies are site-specific. That is, your scripts can read only the cookies that have been created by pages on your site. Similarly, scripts on other sites can read only their own cookies. It isn't possible for one site to create, read, change, or delete cookies that belong to another site. In this sense, cookies are a perfectly secure mechanism for storing data.

CAUTION

In a different sense, however, cookies are anything but secure. As you'll see a bit later, cookies are stored on the user's computer as plain text. This means that if you store sensitive data such as a password or credit card number, anyone who has access to the computer also has easy access to this data. Later in this article, I'll show you how to encode or encrypt cookie data for a higher (but still not perfect) level of security.

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

There are currently no related podcasts. Please check back later.

There are currently no related blogs. Please check back later.

There are currently no related articles. Please check back later.

Informit Network