Complete installation guide for the HTTP Cookie Library.
Version 2.1 | Created 07/13/96 | Last Modified 12/23/96
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:
require '/path/to/cookie.lib'; in your script.
| Variable | Description |
|---|---|
$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. |
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 | Description |
|---|---|
&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. |
print "Content-type: text/html\n";
&SetCookies('name',"$name",'email',"$email");
print "\n";
if (&GetCookies('visit')) {
# Cookie found - $Cookies{'visit'} is set
}
else {
# No cookie - set one
}
Version 2.1 includes URL-encoding for special characters that could cause problems:
%, +, ;, ,, =, &, ::, and spacesThis ensures cookies are set correctly even with special characters in values.
An example script bundled with the library demonstrating cookie usage.
Find real-world implementations in the Scripts Around the World section.