You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Boysenberry Payne <bo...@humaniteque.com> on 2005/02/18 19:43:00 UTC
Looking for easy sessions using mod_perl
I'm moving from PHP to mod_perl. What a jump...
Normally in php it's as simple as:
session_start();
Then depending on your settings the session is kept in a cookie
or in the url.
I've looked into Apache::Session a bit, also Session (which is a
wrapper for Apache:Session.)
I set it up locally using my mysql database, but I'm getting now where
fast.
Here is the code:
my $cookie = $r->header_in( 'Cookie' );
$r->print( "Content Type: text/plain\n\n" );
$cookie =~ s/SESSION_ID=(\w+)/$1/;
my %session;
eval {
tie %session, 'Apache::Session::MySQL', $cookie,
{ DataSource => 'dbi:mysql:boysie_habitat2',
UserName => 'boysie_human',
Password => "h\@b!T",
LockDataSource => 'dbi:mysql:boysie_habitat2',
LockUserName => 'boysie_human',
LockPassword => "h\@b!T",
};
};
if( $@ ) {
$r->print( "Couldn't tie session: $@" );
}
I get the following error:
null: Use of uninitialized value in substitution (s///) at
/Users/WebRoot/perl/index.pl line 16.
I've tried printing out the cookie to see it and get the same error for
anything I try to print it with.
It seems like it's being recorded ok in mysql. I get the id fine, but
the a_session column seems to be funky.
I'll copy an example below:
---------------------------------------
e16d4be4a2f73b323184e8da1cef694d_session_id
---------------------------------------
You can't see it in the copy but there are a bunch of squares (I guess
mac os x's new line characters.)
Any reason why this is so difficult to get working (besides my
inexperience...)
Thanks,
Boysenberry
Re: Looking for easy sessions using mod_perl
Posted by Jonathan Vanasco <jv...@mastersofbranding.com>.
Thats a whole lot easier than my approach.
On Feb 18, 2005, at 2:18 PM, Perrin Harkins wrote:
> It's that easy with mod_perl too if you use Apache::SessionManager.
> Save yourself some trouble and just use that.
>
> http://search.cpan.org/search?query=Apache-SessionManager&mode=dist
Re: Looking for easy sessions using mod_perl
Posted by Perrin Harkins <pe...@elem.com>.
On Fri, 2005-02-18 at 12:43 -0600, Boysenberry Payne wrote:
> Normally in php it's as simple as:
> session_start();
It's that easy with mod_perl too if you use Apache::SessionManager.
Save yourself some trouble and just use that.
http://search.cpan.org/search?query=Apache-SessionManager&mode=dist
- Perrin
Re: Looking for easy sessions using mod_perl
Posted by Jonathan Vanasco <jv...@mastersofbranding.com>.
the squares are because your cookie is serialized -- thats just the
data structure
Below is a bunch of stuff that i routinely use in a class that is
essentially a WebPageVisitor
(its not the complete stuff, so it might seem spotty)
maybe that will help you
---
our $CookieDefaults =
{
domain => [
'127.0.0.1',
],
path => '/',
expires => '+365d',
secure => 0,
};
my $cookiejar = Apache::Cookie::Jar->new( $this->{'ApacheRequestRec'}
);
my @names = $cookiejar->cookies();
if ( $cookiejar->cookies('SESSION_ID') )
{
$sessionID = $this->session__validate(
$cookiejar->cookies('SESSION_ID')->value );
}
sub session__validate
{
my $this = $_[0];
my $sessionID = $_[1];
# If not 32 chars long, kill it
if ( length($sessionID) != 32 ) { $sessionID = undef; }
# If its not in a valid file, kill it
if ( defined($sessionID) )
{
my $DIR =
"${$this->{'ApacheSessionOptions'}}->{Directory}/$sessionID";
if ( -e
"${$this->{'ApacheSessionOptions'}}->{Directory}/$sessionID" ) {
}
else
{
$sessionID = undef;
}
}
return $sessionID;
}
sub session__load
{
my $this = $_[0];
my $sessionID = $_[1];
$this->session__tie( $sessionID );
}
sub session__new
{
my $this = $_[0];
$this->session__tie( '' );
$this->cookie__session__set();
return $this->{'sessionID'}
}
sub session__tie
{
my $this = $_[0];
my $sessionID = $_[1];
tie %{$this->{'__SESSION'}} , 'Apache::Session::File', $sessionID,
${$this->{'ApacheSessionOptions'}};
$this->{'sessionID'} = $this->{'__SESSION'}{_session_id};
}
sub session__new__if_no_cookie
{
my $this = $_[0];
my $cookiejar = $_[1];
if ( !$this->login_from_cookie( $cookiejar ) )
{
$this->cookie__autologin__kill();
}
$this->session__new();
}
sub cookie__session__set
{
my $this = $_[0];
foreach $DOMAIN (@{${$this->{'CookieDefaults'}}->{'domain'}})
{
my $session_cookie = Apache::Cookie->new (
$this->{'ApacheRequestRec'},
-name => 'SESSION_ID',
-value => $this->{'sessionID'},
-domain => $DOMAIN,
-path => ${$this->{'CookieDefaults'}}->{'path'},
-expires => ${$this->{'CookieDefaults'}}->{'expires'},
-secure => ${$this->{'CookieDefaults'}}->{'secure'},
);
# Bake it!
# $session_cookie->bake();
$this->{'ApacheRequestRec'}->err_headers_out->add('Set-Cookie' =>
$session_cookie);
}
}