Frequently asked questions about HTTP cookies and persistent client state.
To use the Cookie Library in your CGI scripts:
require "cookielib.pl";
&SetCookie("cookie_name", "value", "30d");
$value = &GetCookie("cookie_name");
&DeleteCookie("cookie_name");
The expiration time is the third parameter in SetCookie(). Use these formats:
| Format | Example | Result |
|---|---|---|
| Minutes | "30m" | Expires in 30 minutes |
| Hours | "2h" | Expires in 2 hours |
| Days | "7d" | Expires in 7 days |
| Months | "1M" | Expires in 1 month |
| Years | "1y" | Expires in 1 year |
Examples:
# Session cookie (no expiration)
&SetCookie("session", $id, "");
# 30-day cookie
&SetCookie("remember", "yes", "30d");
# 1-year cookie
&SetCookie("prefs", $data, "1y");
Use the extended SetCookieExt() function to set path and domain:
&SetCookieExt($name, $value, $expire, $path, $domain, $secure);
Parameters:
Examples:
# Cookie for entire site
&SetCookieExt("user", $id, "30d", "/", "", 0);
# Cookie for /admin/ section only
&SetCookieExt("admin", $token, "1h", "/admin/", "", 1);
# Cookie for all subdomains
&SetCookieExt("session", $data, "1d", "/", ".example.com", 0);