Recipe 21.2 Setting Cookies
use Apache::Cookie; $cookie = Apache::Cookie->new($r, -name => "cookie name", -value => "its value", -expires => "+1d" ); $cookie->bake;
Don't forget to send the headers before generating content:
The Apache::Cookie module builds a string that represents a cookie. To specify an expiration time for your cookie, use one of these formats:
Call the bake method once you finish changing the cookie's parameters. This adds the cookie in its current state to mod_perl's planned response. If you change the cookie object after calling bake, changes won't be reflected in the header sent by mod_perl.
Apache maintains two sets of headers: error headers (which, confusingly, are always sent, regardless of whether the response code indicates an error) and ordinary headers (which are sent only for successful responses). Apache::Cookie sets the cookie in the error headers, so the cookie will be sent even for redirection responses.
The CGI::Cookie module is a slower pure Perl module with the same interface, which should be used only when the XS module Apache::Cookie is unavailable. To use it, substitute "CGI::Cookie" for "Apache::Cookie" in your code and remove the request object from the call to new. You can't call bake either—instead you must say:
We use err_headers_out rather than err_header_out because the former method allows for multiple values for a header, whereas the latter does not. That is, err_headers_out lets you build up a header over time by adding to its existing value before it's sent, as we might do here if we independently set three cookies. The err_header_out method always replaces, never augments.
21.2.4 See Also
The documentation for the CGI::Cookie and Apache::Cookie modules on CPAN; the Netscape cookie specification at http://wp.netscape.com/newsref/std/cookie_spec.html; Recipe 3.7 in mod_perl Developer's Cookbook; the Apache.pm manpage