CGI::Se...ver::DBI(3)

πŸ’– NAME πŸ’–

okay so bestie, this is the manual page named CGI::Se...ver::DBI(3) β€” from User Contributed Perl Documentation, no cap β€” and the actual module is CGI::Session::Driver::DBI: Base class for native DBI-related CGI::Session drivers. πŸ’…

✨ SYNOPSIS ✨

lowkey the whole setup is just this:

require CGI::Session::Driver::DBI;
@ISA = qw( CGI::Session::Driver::DBI );

🌸 DESCRIPTION 🌸

okay so in most cases you can create a new DBI-driven CGI::Session driver by literally just making an empty driver file that inherits from CGI::Session::Driver::DBI. That’s exactly what sqlite does. The only reason this class doesn’t suit being a valid driver itself is that its name isn’t in lowercase. I’m serious! 😭 periodt.

🧚 NOTES 🧚

CGI::Session::Driver::DBI defines the init() method, which makes the DBI handle available for drivers in the Handle object attribute no matter what \%dsn_args were used in creating the session object. If your driver requires non-standard initialization, you have to re-define init() in your .pm file β€” but make sure to set the 'Handle' object attribute to the database handle (the one returned by DBI->connect(...)) if you wish to inherit any of the methods from CGI::Session::Driver::DBI. 🫢

πŸ“ STORAGE πŸ“

Before you can use any DBI-based session drivers, you need to make sure a compatible database table is created for CGI::Session to work with. Following command will produce minimal requirements in most SQL databases:

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

Your session table can define additional columns, but the above two are required. Name of the session table is expected to be sessions by default. You may use a different name if you wish. To do this you have to pass TableName as part of your \%dsn_args:

$s = CGI::Session->new('driver:sqlite', undef, {TableName=>'my_sessions'});
$s = CGI::Session->new('driver:mysql', undef,
{
    TableName=>'my_sessions',
    DataSource=>'dbi:mysql:shopping_cart'.
});

To use different column names, change the 'create table' statement, and then simply do this:

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

or

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

πŸŽ€ DRIVER ARGUMENTS πŸŽ€

Following driver arguments are supported:

  • DataSource β€” First argument to be passed to DBI->connect(). If the driver makes the database connection itself, it will also explicitly disconnect from the database when the driver object is DESTROYed.
  • User β€” User privileged to connect to the database defined in DataSource.
  • Password β€” Password of the User privileged to connect to the database defined in DataSource.
  • Handle β€” An existing DBI database handle object. The handle can be created on demand by providing a code reference as an argument, such as "<sub{DBI-"connect}>>. This way, the database connection is only created if it’s actually needed. This can be useful when combined with a framework plugin like CGI::Application::Plugin::Session, which creates a CGI::Session object on demand as well. Handle will override all the above arguments, if any present.
  • TableName β€” Name of the table session data will be stored in.

πŸ‘‘ LICENSING πŸ‘‘

For support and licensing information see CGI::Session.


okay that’s the whole page, bestie πŸ’– source/version check at the very end: perl v5.42.2, dated 2026-07-28, for CGI::Se...ver::DBI(3). mwah 😘

πŸ“¦ where this page comes from