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
- alpine 3.24: perl-cgi-session-doc 4.49-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms