Write Your First HTTP Cookie

Learn How to Write and Read an HTTP Cookie

Website Cookies Concept
NiroDesign / Getty Images

Cookies are set by the browser, often with a CGI or JavaScript. You can write a script to set a cookie at any event on a Web page. For example, if you go to this page you will be given the option to set a cookie when you click another link. The cookie looks something like this:

This means:

  • Set-Cookie:
    • This is the call that set's the cookie in the browser's cookie store.
  • Count=1;
    • This is the name of your cookie.
  • expires=Wednesday, 01-Aug-2040 08:00:00 GMT;
    • This details when the cookie will expire.
  • path=/;
    • This is the minimum path that needs to exist for the cookie to be returned.
  • webdesign.about.com
    • The domain that set the cookie, and is the only domain that can retrieve the cookie.

Write the Cookie With JavaScript

Use the following code to write your cookie:

Read Your Cookie

Once you've written the cookie, you need to read it in order to use it. Use this script to read your cookie:

Call Your Cookie in a Link

Set your cookie when someone clicks a link with this code in your HTML body: