new
Apache2::Cookie->new($env, %args)
Just like CGI::Cookie::new, but requires an additional environment argument:
$cookie = Apache2::Cookie->new($r,
-name => 'foo',
-value => 'bar',
-expires => '+3M',
-domain => '.capricorn.com',
-path => '/cgi-bin/database',
-secure => 1
);
The "-value" argument may be either an arrayref, a hashref, or a string. "Apache2::Cookie::freeze"
encodes this argument into the cookie's raw value.
freeze
Apache2::Cookie->freeze($value)
Helper function (for "new") that serializes a new cookie's value in a manner compatible with CGI::Cookie
(and Apache2::Cookie 1.X). This class method accepts an arrayref, hashref, or normal perl string in
$value.
$value = Apache2::Cookie->freeze(["2+2", "=4"]);
thaw
Apache2::Cookie->thaw($value)
$cookie->thaw()
This is the helper method (for "value") responsible for decoding the raw value of a cookie. An optional
argument $value may be used in place of the cookie's raw value. This method can also decode cookie
values created using CGI::Cookie or Apache2::Cookie 1.X.
print $cookie->thaw; # prints "bar"
@values = Apache2::Cookie->thaw($value); # ( "2+2", "=4" )
as_string
$cookie->as_string()
Format the cookie object as a string. The quote-operator for Apache2::Cookie is overloaded to run this
method whenever a cookie appears in quotes.
ok "$cookie" eq $cookie->as_string;
name
$cookie->name()
Get the name of the cookie.
value
$cookie->value()
Get the (unswizzled) value of the cookie:
my $value = $cookie->value;
my @values = $cookie->value;
Note: if the cookie's value was created using a "freeze" method, one way to reconstitute the object is
by subclassing Apache2::Cookie with a package that provides the associated "thaw" sub:
{
package My::COOKIE;
@ISA = 'Apache2::Cookie';
sub thaw { my $val = shift->raw_value; $val =~ tr/a-z/A-Z/; $val }
}
bless $cookie, "My::COOKIE";
ok $cookie->value eq "BAR";
raw_value
$cookie->raw_value()
Gets the raw (opaque) value string as it appears in the incoming "Cookie" header.
ok $cookie->raw_value eq "bar";
bake
$cookie->bake($r)
Adds a Set-Cookie header to the outgoing headers table.
bake2
$cookie->bake2($r)
Adds a Set-Cookie2 header to the outgoing headers table.
domain
$cookie->domain()
$cookie->domain($set)
Get or set the domain for the cookie:
$domain = $cookie->domain;
$cookie->domain(".cp.net");
path
$cookie->path()
$cookie->path($set)
Get or set the path for the cookie:
$path = $cookie->path;
$cookie->path("/");
version
$cookie->version()
$cookie->version($set)
Get or set the cookie version for this cookie. Netscape spec cookies have version = 0; RFC-compliant
cookies have version = 1.
ok $cookie->version == 0;
$cookie->version(1);
ok $cookie->version == 1;
expires
$cookie->expires()
$cookie->expires($set)
Get or set the future expire time for the cookie. When assigning, the new value ($set) should match
/^\+?(\d+)([YMDhms]?)$/ $2 qualifies the number in $1 as representing "Y"ears, "M"onths, "D"ays, "h"ours,
"m"inutes, or "s"econds (if the qualifier is omitted, the number is interpreted as representing seconds).
As a special case, $set = "now" is equivalent to $set = "0".
my $expires = $cookie->expires;
$cookie->expires("+3h"); # cookie is set to expire in 3 hours
secure
$cookie->secure()
$cookie->secure($set)
Get or set the secure flag for the cookie:
$cookie->secure(1);
$is_secure = $cookie->secure;
$cookie->secure(0);
httponly
$cookie->httponly()
$cookie->httponly($set)
Get or set the HttpOnly flag for the cookie:
$cookie->httponly(1);
$is_HttpOnly = $cookie->httponly;
$cookie->httponly(0);
comment
$cookie->comment()
$cookie->comment($set)
Get or set the comment field of an RFC (Version > 0) cookie.
$cookie->comment("Never eat yellow snow");
print $cookie->comment;
commentURL
$cookie->commentURL()
$cookie->commentURL($set)
Get or set the commentURL field of an RFC (Version > 0) cookie.
$cookie->commentURL("http://localhost/cookie.policy");
print $cookie->commentURL;
fetch
Apache2::Cookie->fetch($r)
Fetch and parse the incoming Cookie header:
my $cookies = Apache2::Cookie->fetch($r); # APR::Request::Cookie::Table ref
It should be noted, that with perl 5.8+ Iterator magic, table is able
to handle multiple cookies of the same name.
my %cookies = Apache2::Cookie->fetch($r);