Not a developer? Go to MovableType.com

Documentation

Setting and Retrieving Cookies

The following methods can be used to set and get cookies for the current user.

$app->bake_cookie(%args)

This method sets a cookie to be stored by the current user's browser.

It receives an associative array, or hash, as input which can contain any valid parameters to the "new" methods of the CGI::Cookie perl module including:

  • "-name"
  • "-value"
  • "-path"
  • "-secure"
  • "-expires"

If you do not include the "-path" parameter in %arg, it will be set automatically to "$app->path".

This method will automatically assign a "secure" flag for the cookie if it the current HTTP request is using the https protocol. To forcibly disable the secure flag, provide a "-secure" argument with a value of 0.

For example, to set a cookie with a name of "foo" and a value of "bar" that expires in 3 months, use the following:

sub handler {
    my $app = shift;
    # do something
    $app->base_cookie( -name => 'foo', -value => 'bar', -expires => '+3M' );
}

$app->cookies()

To access all the cookies associated with a request, the $app->cookies() method will return a hash containing every cookie. The keys of the hash correspond to the cookie names, and their corresponding values are a complete CGI::Cookie object giving you access to the expiration date, cookie path, cookie domain and other cookie properties.

$app->cookie_val

For expediency, if you want to simply retrieve the value of a cookie transmitted from the client, use the $app->cookie_val($name) method. Pass in the cookie name as an argument and receive its value.

See Also

Back

Leave a Comment