![]() |
![]() |
||||||
|
|||||||
POD documentation > Web Protocols > Cookie.pm posted on 3:04 PM, July 12, 2009
ExSite Cookie ManagementExSite::Cookie manages your cookie jar, which is a hash (
if ($cookie{foo} eq "bar") { ... }
To set a cookie named ``foo'' to value ``bar'':
$cookie{foo} = "bar";
If this creates a new cookie name or changes the value of an existing
cookie, then ExSite will automatically issue a set-cookie: header so
the browser will remember this cookie setting on future requests. The
set-cookie header will use automatic expiry, path, and domain
settings. Default behaviour is to expire after the browser is closed,
and to be valid for the default ExSite domain (
To set a cookie with custom expiry, path, and domain settings, do this:
(tied %cookie)->set_cookie($name,$value,$path,$domain,$expiry);
(tied %cookie)->cookie_header($name,$value,$path,$domain,$expiry);
To unset (clear) a cookie named ``foo'', use one of:
delete $cookie{foo};
$cookie{foo} = undef;
To temporarily set a cookie for this request only, but not for subsequent requests, do this:
(tied %cookie)->store($name,$value); # set
(tied %cookie)->store($name,undef); # unset
This has the effect of altering the contents of
(tied %cookie)->store_update($name,$value); # set
(tied %cookie)->store_update($name,undef); # unset
Cookie ScopeCookies normally apply to a specific domain and server path. The default cookie jar sets these automatically to:
domain = $config{server}{domain}
path = $config{server}{CGIpath}
Cookies not valid in this scope will not be found in the cookie jar.
Cookies set using normal hash settings (eg.
To create an alternate cookie jar with a different scope, simply declare a new cookie hash, and tie it to this class with the new path and domain:
tie %my_cookie, 'ExSite::Cookie', $path, $domain;
Long-Duration CookiesBy default, cookies expire when the browser is closed. You can optionally set durable cookies as follows:
(tied %cookie)->store_remember($key,$val);
This will set an explicit expiry time for the cookie. However, you must tell your cookie jar what this duration should be, when you set it up:
tie %cookie, 'ExSite::Cookie', $path, $domain, "2 weeks";
The duration is specified as a string of the format ``NUMBER
TIME_UNITS'', eg. ``7 days'', or ``2 weeks'', or ``3 months''. The default
cookie jar uses the configuration setting
(tied %cookie)->store_remember($key,$val,$expiry);
To get a date string in the correct format for
my $t = new ExSite::Time; # now
$t->add(2,"weeks"); # 2 weeks in future
my $expiry = $t->write("cookie"); # a date string in cookie format
Internals
The cookie object has the following internal attributes:
|
Recent ArticlesDocumentation Topicsbest practices (5)
content management (12)
data handling (7)
fundamentals (3)
google (5)
graphic design (21)
html formatting (7)
IT (9)
plug-in modules (28)
POD (32)
programming (48)
RSS (3)
security (3)
SEO (3)
visual tutorial (29)
web protocols (9)
|