NAME

CGI::Session::MySQL - Driver for CGI::Session class

SYNOPSIS

use constant COOKIE => "TEST_SID";	# cookie to store the session id

use CGI::Session::MySQL;
use CGI;
use DBI;

my $dbh = DBI->connect("DBI:mysql:dev", "dev", "marley01");
my $cgi = new CGI;

# getting the session id from the cookie
my $c_sid = $cgi->cookie(COOKIE) || undef;

my $session = new CGI::Session::MySQL($c_sid, 
	{
		LockHandle		=> $dbh,
		Handle			=> $dbh
	});

# now let's create a sid cookie and send it to the client's browser.
# if it is an existing session, it will be the same as before,
# but if it's a new session, $session->id() will return a new session id.
{
	my $new_cookie = $cgi->cookie(-name=>COOKIE, -value=>$session->id);
	print $cgi->header(-cookie=>$new_cookie);
}

print $cgi->start_html("CGI::Session::MySQL");

# assuming we already saved the users first name in the session
# when he visited it couple of days ago, we can greet him with
# his first name

print "Hello", $session->param("f_name"), ", how have you been?";

print $cgi->end_html();

DESCRIPTION

CGI::Session::MySQL is the driver for the CGI::Session class to store and retrieve the session data in and from the MySQL database

To be able to write your own drivers for CGI::Session, please consult CGI::Session manual.

Constructor requires two arguments, as all other CGI::Session drivers do. The first argument has to be session id to be initialized (or undef to tell the CGI::Session to create a new session id). The second argument has to be a reference to a hash with two following required key/value pairs:

Handle

this has to be a database handler returned from the DBI-connect()> (see DBI manual, DBD::mysql manual)

LockHandle

This is also a handler returned from the DBI-connect()> for locking the sessions table.

You can also ask CGI::Session::MySQL to create a handler for you. To do this you will need to pass it the following key/value pairs as the second argument:

DataSource

Name of the datasource DBI has to use. Usually DBI:mysql:db_name

UserName

Username who is able to access the above DataSource

Password

Password the UserName has to provide to be able to access the DataSource.

It also expects LockDatasource, LockUserName and LockPassword key/values, but if they are missing defaults to the ones provided by DataSource, UserName and Password respectively.

CGI::Session::MySQL uses Data::Dumper to serialize the session data before storing it in the session file.

STORAGE

Since the data should be stored in the mysql table, you will first need to create a sessions table in your mysql database. The following command should suffice for basic use of the library:

CREATE TABLE sessions (
	id CHAR(32) NOT NULL PRIMARY KEY,
	a_session TEXT
);

Example

# get the sessino id either from the SID cookie, or from
# the sid parameter in the URL
my $c_sid = $cgi->cookie("SID") || $cgi->param("sid") || undef;
my $session = new CGI::Session::MySQL($c_sid, 
	{
		Handle => $dbh,
		LockHandle => $dbh
	});

For more extensive examples of the CGI::Session usage, please refer to the manual

BUGS

Currently no bugs have been detected, but eval() in retrive() method produces the following error:

Use of uninitialized value in string at blib/lib/CGI/Session/MySQL.pm line 65.

Of which I am quite embarrased. Coulld not figure out how to get rid of it. So if you can think of any solution, please take this guilt off my conscious ASAP.

AUTHOR

Sherzod B. Ruzmetov <sherzodr@cpan.org>

COPYRIGHT

This library is free software and can be redistributed under the same conditions as Perl itself.

SEE ALSO

CGI::Session, CGI::Session::File, CGI::Session::DB_File, CGI::Session::MySQL, Apache::Session