1 package Catalyst::Plugin::Session::State::Cookie;
2 use base qw/Catalyst::Plugin::Session::State/;
8 use Catalyst::Utils ();
10 our $VERSION = "0.04";
15 $c->NEXT::setup_session(@_);
17 $c->config->{session}{cookie_name}
18 ||= Catalyst::Utils::appprefix($c) . '_session';
21 sub extend_session_id {
22 my ( $c, $sid, $expires ) = @_;
24 if ( my $cookie = $c->get_session_cookie ) {
25 $c->update_session_cookie( $c->make_session_cookie( $sid ) );
28 $c->NEXT::extend_session_id( @_ );
34 $c->update_session_cookie( $c->make_session_cookie( $sid ) );
36 return $c->NEXT::set_session_id(@_);
39 sub update_session_cookie {
40 my ( $c, $updated ) = @_;
41 my $cookie_name = $c->config->{session}{cookie_name};
42 $c->response->cookies->{$cookie_name} = $updated;
45 sub make_session_cookie {
46 my ( $c, $sid, %attrs ) = @_;
48 my $cfg = $c->config->{session};
51 ( $cfg->{cookie_domain} ? ( domain => $cfg->{cookie_domain} ) : () ),
55 unless ( exists $cookie->{expires} ) {
56 $cookie->{expires} = $c->calculate_session_cookie_expires();
59 $cookie->{secure} = 1 if $cfg->{cookie_secure};
64 sub calc_expiry { # compat
66 $c->NEXT::calc_expiry( @_ ) || $c->calculate_session_cookie_expires( @_ );
69 sub calculate_session_cookie_expires {
71 my $cfg = $c->config->{session};
73 my $value = $c->NEXT::calculate_session_cookie_expires(@_);
74 return $value if $value;
76 if ( exists $cfg->{cookie_expires} ) {
77 if ( $cfg->{cookie_expires} > 0 ) {
78 return time() + $cfg->{cookie_expires};
85 return $c->session_expires;
89 sub get_session_cookie {
92 my $cookie_name = $c->config->{session}{cookie_name};
94 return $c->request->cookies->{$cookie_name};
100 if ( my $cookie = $c->get_session_cookie ) {
101 my $sid = $cookie->value;
102 $c->log->debug(qq/Found sessionid "$sid" in cookie/) if $c->debug;
106 $c->NEXT::get_session_id(@_);
109 sub delete_session_id {
110 my ( $c, $sid ) = @_;
112 $c->update_session_cookie( $c->make_session_cookie( $sid, expires => 0 ) );
114 $c->NEXT::delete_session_id($sid);
125 Catalyst::Plugin::Session::State::Cookie - Maintain session IDs using cookies.
129 use Catalyst qw/Session Session::State::Cookie Session::Store::Foo/;
133 In order for L<Catalyst::Plugin::Session> to work the session ID needs to be
134 stored on the client, and the session data needs to be stored on the server.
136 This plugin stores the session ID on the client using the cookie mechanism.
142 =item make_session_cookie
144 Returns a hash reference with the default values for new cookies.
146 =item update_session_cookie $hash_ref
148 Sets the cookie based on C<cookie_name> in the response object.
152 =head1 EXTENDED METHODS
156 =item prepare_cookies
158 Will restore if an appropriate cookie is found.
160 =item finalize_cookies
162 Will set a cookie called C<session> if it doesn't exist or if it's value is not
163 the current session id.
167 Will set the C<cookie_name> parameter to it's default value if it isn't set.
177 The name of the cookie to store (defaults to C<Catalyst::Utils::apprefix($c) . '_session'>).
181 The name of the domain to store in the cookie (defaults to current host)
185 Number of seconds from now you want to elapse before cookie will expire.
186 Set to 0 to create a session cookie, ie one which will die when the
187 user's browser is shut down.
191 If this attribute set true, the cookie will only be sent via HTTPS.
197 Sessions have to be created before the first write to be saved. For example:
200 my ( $self, $c ) = @_;
201 $c->res->write("foo");
206 Will cause a session ID to not be set, because by the time a session is
207 actually created the headers have already been sent to the client.
211 L<Catalyst>, L<Catalyst::Plugin::Session>.
215 This module is derived from L<Catalyst::Plugin::Session::FastMmap> code, and
216 has been heavily modified since.
221 Yuval Kogman, C<nothingmuch@woobling.org>
227 This program is free software, you can redistribute it and/or modify it
228 under the same terms as Perl itself.