The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

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

SYNOPSIS

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

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

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


    # For examples see CGI::Session manual

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 developer section of 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. If it is not set should default to Handle

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
    );

sessions is the default name CGI::Session::MySQL would work with. We suggest you to stick with this name for consistency. In case for any reason you want to use a different name, just update $CGI::Session::MySQL::TABLE_NAME variable before creating the object:

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

    $CGI::Session::MySQL::TABLE_NAME = 'my_sessions';
    $dbh = DBI->connect("dbi:mysql:dev", "dev", "marley01");

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

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