CGI::Se...r::mysql(3)

πŸ’… CGI::Session::Driver::mysql(3) β€” the User Contributed Perl Documentation glow-up πŸŽ€

Okay so bestie, you're looking at the CGI::Session::Driver::mysql page from the User Contributed Perl Documentation collection. Pull up a chair, grab a strawberry πŸ“, and let's get into it.


✨ NAME ✨

CGI::Session::Driver::mysql β€” it's a CGI::Session driver for a MySQL database. That's the whole vibe. πŸ’–


πŸ¦‹ SYNOPSIS πŸ¦‹

Here's how you actually call this thing, copy-paste energy:

$s = CGI::Session->new( 'driver:mysql', $sid);
$s = CGI::Session->new( 'driver:mysql', $sid, { DataSource  => 'dbi:mysql:test',
                                               User        => 'sherzodr',
                                               Password    => 'hello' });
$s = CGI::Session->new( 'driver:mysql', $sid, { Handle => $dbh } );

Three ways, all iconic, pick your fighter. πŸ‘‘


🌸 DESCRIPTION 🌸

So mysql stores your session records in a MySQL table. That's it, that's the gig. For the deets you gotta go see CGI::Session::Driver::DBI, which is literally its parent class β€” the main character it inherited everything from.

Heads up though, this part is important: with the MySQL driver it's especially important that your session ID column is defined as a primary key, or at the very least unique. Like this, no cap:
 CREATE TABLE sessions (
     id CHAR(32) NOT NULL PRIMARY KEY,
     a_session TEXT NOT NULL
  );

Wanna use different column names? Bet. πŸ’… Just change that 'create table' statement, and then simply do this:

    $s = CGI::Session->new('driver:mysql', undef,
    {
        TableName=>'session',
        IdColName=>'my_id',
        DataColName=>'my_data',
        DataSource=>'dbi:mysql:project',
    });

or

    $s = CGI::Session->new('driver:mysql', undef,
    {
        TableName=>'session',
        IdColName=>'my_id',
        DataColName=>'my_data',
        Handle=>$dbh,
    });

Two paths, same destiny. ✨


🧚 DRIVER ARGUMENTS 🧚

The mysql driver supports all the arguments documented in CGI::Session::Driver::DBI. All of them. Every single one, we don't gatekeep here.

Plus β€” and this is lowkey cute β€” the DataSource argument can optionally leave the leading "dbi:mysql:" string out entirely:

    $s = CGI::Session->new( 'driver:mysql', $sid, {DataSource=>'shopping_cart'});
    # is the same as:
    $s = CGI::Session->new( 'driver:mysql', $sid, {DataSource=>'dbi:mysql:shopping_cart'});

Same thing, one's just wearing less makeup. πŸ’„


πŸ’€ BACKWARDS COMPATIBILITY πŸ’€

As of V 4.30, the global variable $CGI::Session::MySQL::TABLE_NAME cannot be used to set the session table's name. It's giving deprecated. It's giving retired. πŸ™ˆ

Why, you ask? Because of changes in CGI::Session::Driver's new() method β€” which now allows the table's name to be changed (and also allows both the 'id' column name and the 'a_session' column name to be changed). Glow-up for everyone. 🫢

See the documentation for CGI::Session::Driver::DBI for details, fr fr.

In particular: the new syntax for new() applies to all database drivers, whereas the old β€” and bad β€” global variable method only applied to MySQL. Yeah. The old way was not it. 😭

Alternately, call $session -> table_name('new_name') just after creating the session object if you wish to change the session table's name. Options, bestie, you've got options. 🌈


πŸ‘‘ LICENSING πŸ‘‘

For support and licensing see CGI::Session. That's the whole section, she's short and to the point. πŸ’œ


✨ That's the page, bestie! ✨ This little gem is brought to you by perl v5.42.2, dated 2026-07-28 β€” go forth and store those sessions, you absolute icon. πŸŽ€πŸ’–πŸ”₯

πŸ“¦ where this page comes from