1 package Catalyst::Plugin::Session::State::Cookie;
2 use base qw/Catalyst::Plugin::Session::State/;
14 $c->NEXT::setup_session(@_);
16 $c->config->{session}{cookie_name} ||= "session";
19 sub finalize_cookies {
23 $c->update_session_cookie( $c->make_session_cookie );
26 return $c->NEXT::finalize_cookies(@_);
29 sub update_session_cookie {
30 my ( $c, $updated ) = @_;
31 my $cookie_name = $c->config->{session}{cookie_name};
32 $c->response->cookies->{$cookie_name} = $updated;
35 sub make_session_cookie {
38 my $cfg = $c->config->{session};
40 value => $c->sessionid,
41 ($cfg->{cookie_domain} ? (domain => $cfg->{cookie_domain}) : ()),
44 if ( exists $cfg->{cookie_expires} ) {
45 if ( my $ttl = $cfg->{cookie_expires} ) {
46 $cookie->{expires} = time() + $ttl;
47 } # else { cookie is non-persistent }
49 $cookie->{expires} = $c->session->{__expires};
58 my $ret = $c->NEXT::prepare_cookies(@_);
60 my $cookie_name = $c->config->{session}{cookie_name};
62 if ( my $cookie = $c->request->cookies->{$cookie_name} ) {
63 my $sid = $cookie->value;
65 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
79 Catalyst::Plugin::Session::State::Cookie - A session ID
83 use Catalyst qw/Session Session::State::Cookie Session::Store::Foo/;
87 In order for L<Catalyst::Plugin::Session> to work the session ID needs to be
88 stored on the client, and the session data needs to be stored on the server.
90 This plugin stores the session ID on the client using the cookie mechanism.
96 =item make_session_cookie
98 Returns a hash reference with the default values for new cookies.
100 =item update_session_cookie $hash_ref
102 Sets the cookie based on C<cookie_name> in the response object.
106 =head1 EXTENDED METHODS
110 =item prepare_cookies
112 Will restore if an appropriate cookie is found.
114 =item finalize_cookies
116 Will set a cookie called C<session> if it doesn't exist or if it's value is not
117 the current session id.
121 Will set the C<cookie_name> parameter to it's default value if it isn't set.
131 The name of the cookie to store (defaults to C<session>).
135 The name of the domain to store in the cookie (defaults to current host)
141 Sessions have to be created before the first write to be saved. For example:
144 my ( $self, $c ) = @_;
145 $c->res->write("foo");
150 Will cause a session ID to not be set, because by the time a session is
151 actually created the headers have already been sent to the client.
155 L<Catalyst>, L<Catalyst::Plugin::Session>.
159 Sebastian Riedel E<lt>C<sri@cpan.org>E<gt>,
160 Marcus Ramberg E<lt>C<mramberg@cpan.org>E<gt>,
161 Andrew Ford E<lt>C<andrewf@cpan.org>E<gt>,
162 Yuval Kogman E<lt>C<nothingmuch@woobling.org>E<gt>
166 This program is free software, you can redistribute it and/or modify it
167 under the same terms as Perl itself.