1 package Catalyst::Plugin::Session::State::Cookie;
2 use base qw/Catalyst::Plugin::Session::State/;
8 use Catalyst::Utils ();
10 our $VERSION = "0.02";
15 $c->NEXT::setup_session(@_);
16 $c->config->{session}{cookie_name}
17 ||= Catalyst::Utils::appprefix($c) . '_session';
20 sub finalize_cookies {
23 if ( $c->sessionid ) {
24 $c->update_session_cookie( $c->make_session_cookie );
27 return $c->NEXT::finalize_cookies(@_);
30 sub update_session_cookie {
31 my ( $c, $updated ) = @_;
32 my $cookie_name = $c->config->{session}{cookie_name};
33 $c->response->cookies->{$cookie_name} = $updated;
36 sub make_session_cookie {
39 my $cfg = $c->config->{session};
41 value => $c->sessionid,
42 ( $cfg->{cookie_domain} ? ( domain => $cfg->{cookie_domain} ) : () ),
45 $cookie->{expires}=$c->calc_expiry();
47 $cookie->{secure} = 1 if $cfg->{cookie_secure};
54 my $cfg = $c->config->{session};
55 my $value= $c->NEXT::calc_expiry(@_);
56 return $value if $value;
57 if ( exists $cfg->{cookie_expires} ) {
58 if ( $cfg->{cookie_expires} > 0 ) {
59 return time() + $cfg->{cookie_expires};
66 return $c->session_expires;
73 my $ret = $c->NEXT::prepare_cookies(@_);
75 my $cookie_name = $c->config->{session}{cookie_name};
77 if ( my $cookie = $c->request->cookies->{$cookie_name} ) {
78 my $sid = $cookie->value;
80 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
94 Catalyst::Plugin::Session::State::Cookie - Maintain session IDs using cookies.
98 use Catalyst qw/Session Session::State::Cookie Session::Store::Foo/;
102 In order for L<Catalyst::Plugin::Session> to work the session ID needs to be
103 stored on the client, and the session data needs to be stored on the server.
105 This plugin stores the session ID on the client using the cookie mechanism.
111 =item make_session_cookie
113 Returns a hash reference with the default values for new cookies.
115 =item update_session_cookie $hash_ref
117 Sets the cookie based on C<cookie_name> in the response object.
121 =head1 EXTENDED METHODS
125 =item prepare_cookies
127 Will restore if an appropriate cookie is found.
129 =item finalize_cookies
131 Will set a cookie called C<session> if it doesn't exist or if it's value is not
132 the current session id.
136 Will set the C<cookie_name> parameter to it's default value if it isn't set.
146 The name of the cookie to store (defaults to C<Catalyst::Utils::apprefix($c) . '_session'>).
150 The name of the domain to store in the cookie (defaults to current host)
154 Number of seconds from now you want to elapse before cookie will expire.
155 Set to 0 to create a session cookie, ie one which will die when the
156 user's browser is shut down.
160 If this attribute set true, the cookie will only be sent via HTTPS.
166 Sessions have to be created before the first write to be saved. For example:
169 my ( $self, $c ) = @_;
170 $c->res->write("foo");
175 Will cause a session ID to not be set, because by the time a session is
176 actually created the headers have already been sent to the client.
180 L<Catalyst>, L<Catalyst::Plugin::Session>.
184 This module is derived from L<Catalyst::Plugin::Session::FastMmap> code, and
185 has been heavily modified since.
190 Yuval Kogman, C<nothingmuch@woobling.org>
196 This program is free software, you can redistribute it and/or modify it
197 under the same terms as Perl itself.