HTTP Cookie Library Readme

Complete installation guide for the HTTP Cookie Library.

Version 2.1 | Created 07/13/96 | Last Modified 12/23/96

Table of Contents

Overview

HTTP Cookie Library is a Perl 4 and 5 compatible library which allows you to easily use Persistent Client State HTTP Cookies by providing subroutines to:

  • Get cookies from the environment
  • Prepare and set cookies
  • Change expiration date, domain and path
  • Compress multiple cookies into one
Files Included
  1. README - Detailed installation instructions
  2. cookie.lib - The Perl library file
  3. ccounter.pl - Example of how to use the library

cookie.lib Configuration

VariableDescription
$Cookie_Exp_Date Expiration date in format: Wdy, DD-Mon-YYYY HH:MM:SS GMT. Leave blank for session cookies.
$Cookie_Path URL path where cookies should be sent (e.g., /your_dir).
$Cookie_Domain Base domain for cookies (e.g., .host.xxx). Requires 2-3 periods.
$Secure_Cookie Set to 1 to restrict transmission to secure servers only.

Using This Library

Requiring the Library

Near the top of your script, include:

require '/path/to/cookie.lib';

Or if in the same directory or in @INC:

require 'cookie.lib';
Subroutine Calls
SubroutineDescription
&GetCookies('name1',...) Get cookies from environment. Returns 1 if found, 0 if not. Values stored in %Cookies.
&SetCookieExpDate('date') Set expiration date. Format: Wdy, DD-Mon-YYYY HH:MM:SS GMT
&SetCookiePath('/path') Set the URL path for the cookie.
&SetCookieDomain('.host.xxx') Set the domain for the cookie.
&SetSecureCookie('0' || '1') Set whether cookie is secure-only.
&SetCookies('name1','val1',...) Set one or more cookies with name/value pairs.
&SetCompressedCookies('name','n1','v1',...) Compress multiple cookies into one to save space (20 cookie limit per domain).
&GetCompressedCookies('name','n1',...) Retrieve cookies from a compressed cookie.

Example Usage

Setting Cookies
print "Content-type: text/html\n";
&SetCookies('name',"$name",'email',"$email");
print "\n";
Getting Cookies
if (&GetCookies('visit')) {
    # Cookie found - $Cookies{'visit'} is set
}
else {
    # No cookie - set one
}

Character Translation

Version 2.1 includes URL-encoding for special characters that could cause problems:

  • %, +, ;, ,, =, &, ::, and spaces

This ensures cookies are set correctly even with special characters in values.

Examples

ccounter.pl

An example script bundled with the library demonstrating cookie usage.

Scripts Around the World

Find real-world implementations in the Scripts Around the World section.

Other Cookie Resources

  • Netscape Cookie Specification - The specifications this library conforms to
  • Andy's Netscape HTTP Cookie Info - Extensive cookie information and FAQs
  • Malcolm's Guide to Persistent Cookies - Security and privacy discussion
  • JavaScript Cookie Functions - Similar library for JavaScript

Version History

2.1 (12/23/96)
Fixed variable scoping, added URL-encoding for special characters, renamed &UnCompressCookies to &GetCompressedCookies.
2.0 (11/28/96)
Added cookie compression subroutines, fixed multiple cookie setting, eliminated &PrepareCookies.
1.1.1 (07/15/96)
Fixed ExpDate year format to YYYY.
1.1 (07/14/96)
Added secure option and routines.
1.0 (07/14/96)
Initial release.
Back to Documentation Cookie Library Overview