PHP::Session::DB - read / write PHP sessions stored in data bases
use PHP::Session::DB; my $session = PHP::Session::DB->new($id, { DBUSER => $dbuser, DBPASSWD => $dbpasswd, DBNAME => $dbname }); # session id my $id = $session->id; # get/set session data my $foo = $session->get('foo'); $session->set(bar => $bar); # remove session data $session->unregister('foo'); # remove all session data $session->unset; # check if data is registered $session->is_registered('bar'); # save session data $session->save; # destroy session $session->destroy; # create a new session, if not existent $session = PHP::Session->new($new_sid, { %dbvars, create => 1 });
PHP::Session::DB provides a way to read / write PHP4 sessions stored on databases, with which you can make your Perl application session shared with PHP4.
Constructor new takes some options as hashref.
new
this is the type of database that will be used. It must be a valid DBI driver. default: mysql.
this is the database name that will store the sessions table. This is a mandatory argument
this is the table that stores the sessions data. default: sessions.
this is the username that will be used to connect to the data base. This is a mandatory argument
DBUSER password. This is a mandatory argument
Database host. default: localhost.
Database port. default: 3306 (mysql default port).
type of serialization handler. Currently only PHP default serialization is supported.
whether to create session file, if it's not existent yet. default: 0
whether to save modification to session file automatically. default: 0
Consider cases like this:
my $session = PHP::Session->new($sid, { auto_save => 1 }); $session->set(foo => 'bar'); # Oops, you forgot save() method!
If you set auto_save to true value and when you forget to call save method after parameter modification, this module would save session file automatically when session object goes out of scope.
auto_save
save
If you set it to 0 (default) and turn warnings on, this module would give you a warning like:
PHP::Session: some keys are changed but not modified.
use strict; use PHP::Session::DB; use CGI::Lite; my $session_name = 'PHPSESSID'; # change this if needed print "Content-type: text/plain\n\n"; my $cgi = new CGI::Lite; my $cookies = $cgi->parse_cookies; if ($cookies->{$session_name}) { my $session = PHP::Session->new($cookies->{$session_name}, {DBUSER => 'uname, DBPASSWD => '123', DBNAME => 'dunno'); # now, try to print uid variable from PHP session print "uid:",Dumper($session->get('uid')); } else { print "can't find session cookie $session_name"; }
If you are using PHP::Session and want to swith to PHP::Session::DB, the only thing you need to change is the way you call the new method. It is necessary that you add the DB arguments (at least DBUSER, DBPASSWD and DBNAME) in order to get the module work properly.
DB
Array in PHP is hash in Perl.
Objects in PHP are restored as objects blessed into PHP::Session::Object (Null class) and original class name is stored in _class key.
_class
Locking when save()ing data is acquired via exclusive flock, same as PHP implementation.
flock
I have tested PHP::Session::DB only in MySQL databases, but you should not have any kind of problem to get it work with another databases. If you have any problem just send me an email.
Testing in databases such as PostgreSQL, Oracle, MSQL and others.
Roberto Alamos Moreno <ralamosm@cpan.org>
based on PHP::Session written by
Tatsuhiko Miyagawa <miyagawa@bulknews.net>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
PHP::Session, Apache::Session::PHP, WDDX, Apache::Session, CGI::kSession
To install PHP::Session::DB, copy and paste the appropriate command in to your terminal.
cpanm
cpanm PHP::Session::DB
CPAN shell
perl -MCPAN -e shell install PHP::Session::DB
For more information on module installation, please visit the detailed CPAN module installation guide.