CGI::S...ssions(3pm)
β¨ NAME β¨
bestie, meet CGI::Session::ExpireSessions β aka CGI::S...ssions(3pm) on the manual shelf π
she expires your CGI::Session db-based and file-based sessions, no cap.
π¦ SYNOPSIS π¦
#!/usr/bin/env perl
use strict;
use warnings;
use CGI::Session::ExpireSessions;
use DBI;
# -----------------------------------------------
my($dbh) = DBI -> connect
(
'dbi:mysql:aussi:127.0.0.1',
'root',
'pass',
{
AutoCommit => 1,
PrintError => 0,
RaiseError => 1,
ShowErrorStatement => 1,
}
);
CGI::Session::ExpireSessions -> new(dbh => $dbh, verbose => 1) -> expire_db_sessions();
CGI::Session::ExpireSessions -> new(temp_dir => '/tmp', verbose => 1) -> expire_file_sessions();
CGI::Session::ExpireSessions -> new(verbose => 1) -> expire_sessions();
# Note: You are strongly urged to use method expire_sessions() (it requires CGI::Session V 4 or later),
# since it does not eval the session data, and hence avoids the security issues of evaling a string
# which comes from outside the program. See examples/expire-set.pl, which contains extensive comments.
πΈ DESCRIPTION πΈ
CGI::Session::ExpireSessions is a pure Perl module, periodt. π
She deletes CGI::Session-type sessions that have fully passed their use-by date. Like, expired, gone, not coming back. π
She works with CGI::Session-type sessions that live in a database or in disk files β but she does not appear to work with CGI::Session::PureSQL-type sessions, and that's just the tea. π
The recommended way to use this module is via method expire_sessions(), which requires CGI::Session V 4 or later. That's the main character route, iykyk. π
Sessions can be expired under one of three conditions:
- You deem the session to be expired as of now β¨
- Methods:
expire_db_sessions()andexpire_file_sessions()- You want the session to be expired and hence deleted now because its last access time is longer ago than the time you specify in the call to
new, using thedeltaparameter. - That is, delete the session because the time span, between the "last access" time and now, is greater than
delta. - In other words, force sessions to expire. π₯
- The module has always used this condition to delete sessions.
- You want the session to be expired and hence deleted now because its last access time is longer ago than the time you specify in the call to
- Method:
expire_sessions()- You want the session to be expired and hence deleted now because its "last access" time is longer ago than the time you specify in the call to
new, using thedeltaparameter.
- You want the session to be expired and hence deleted now because its "last access" time is longer ago than the time you specify in the call to
- Methods:
- The session has already expired π«Ά
- This section applies to all 3 methods:
expire_db_sessions(),expire_file_sessions()andexpire_sessions(). - This condition is new as of V 1.02.
- You want the session to be deleted now because it has already expired.
- That is, you want this module to delete the session, rather than getting
CGI::Sessionto delete it, whenCGI::Sessionwould delete the session automatically if you usedCGI::Sessionto retrieve the session. - Note: This condition assumes the session's expiration time is defined (it does not have to be).
- This section applies to all 3 methods:
- The file size is
<= 5 bytesand was accessed more than'delta'seconds ago π§- This condition is new as of V 1.03.
- This section applies to method:
expire_file_sessions(). - See below for how to provide a value of
deltato the constructor. - Old versions of
CGI::Sessionsometimes create a file of size 0 bytes, so this test checks for such files, and deletes them if they are old enough.
Sessions are deleted if any of these conditions is true. π
Sessions are deleted from the 'sessions' table in the database, or from the temp directory, depending on how you use CGI::Session.
π¦ DISTRIBUTIONS π¦
This module is available both as a Unix-style distro (*.tgz) and an ActiveState-style distro (*.ppd). The latter is shipped in a *.zip file. Two flavors, both iconic. β¨
See http://savage.net.au/Perl-modules.html for details.
See http://savage.net.au/Perl-modules/html/installing-a-module.html for help on unpacking and installing each type of distro.
π SECURITY π
okay bestie, listen up, this part is serious π¨
For file-based sessions, method expire_file_sessions() parses the contents of the file, using eval{}, in an attempt to determine the access and expiration times recorded within the file. π
So, if you are uneasy about the security implication of this (as you should be), don't use this method. Use method expire_sessions() instead. The latter is a much more sophisticated way of expiring sessions, but it does require CGI::Session V 4 or later. slay responsibly. π«Ά
π CONSTRUCTOR AND INITIALIZATION π
new(...) returns a CGI::Session::ExpireSessions object.
This is the class's contructor. (yes, that's how it's spelled, we don't fix it, we keep it π)
Usage: CGI::Session::ExpireSessions -> new().
This method takes a set of parameters. Only some of these parameters are mandatory.
For each parameter, call method new() as new(param_1 => value_1, param_2 => value_2, ...).
Note: As of V 1.07 of this module, you may call method set() to set parameters after calling method new().
Not only that, but you may pass into all of the 3 methods expire_db_sessions(), expire_file_sessions() and expire_sessions() any of the parameters accepted by new(), since these 3 methods call set() if their caller provides parameters. so flexible, we love her π
Parameters which can be used with new(), set(), or expire_*():
cgi_session_dsnβ This is the DSN (Data Source Name) used byCGI::Sessionto control what type of sessions you previously created and what type of sessions you now wish to expire. Do not confuse this with the DSN used byCGI::Session's methodfind(param_1, \&sub, {DataSource => other_dsn...}, ...)when referring to db-based sessions. Methodexpire_sessions()is the only method in this module which uses this parameter. So, when you callexpire_sessions(), this parameter βcgi_session_dsnβ determines the set of sessions processed by, and possibly expired by, the call toexpire_sessions(). The default value isundef, which meansCGI::Sessiondefaults to file-based sessions. This parameter is optional for file-based sessions, and mandatory for db-based sessions.dbhβ This is a database handle for the database containing the table'sessions'. Either this parameter is mandatory, or thetemp_dirparameter is mandatory. π«‘delta- Methods:
expire_db_sessions()andexpire_file_sessions()- This is the number of seconds since the "last access" to the session, which determines whether or not the session will be expired.
- Method:
expire_sessions()- This is the number of seconds since the "last access" time of the session, which determines whether or not the session will be expired.
- The default value is
2 * 24 * 60 * 60, which is the number of seconds in 2 days. - By default, then, sessions which were last accessed more than 2 days ago are expired.
- This parameter is optional.
- Methods:
dsn_argsβ If yourcgi_session_dsnuses file-based storage, then this hashref might contain keys such as:
{
Directory => Value 1,
NoFlock => Value 2,
UMask => Value 3
}
If your cgi_session_dsn uses db-based storage, then this hashref contains (up to) 3 keys, and looks like:
{
DataSource => Value 1,
User => Value 2,
Password => Value 3
}
These 3 form the DSN, username and password used by DBI to control access to your database server, and hence are only relevant when using db-based sessions. Method expire_sessions() is the only method in this module which uses the parameter dsn_args. The default value for this parameter is undef. These parameters are optional for file-based sessions, and mandatory for db-based sessions.
table_nameβ This is the name of the database table used to hold the sessions. The default value is'sessions'. This parameter is optional. πtemp_dirβ This is the name of the temp directory where you storeCGI::Session-type session files. The default value is'/tmp'. Either this parameter is mandatory, or thedbhparameter is mandatory.timeβ The session's "last access" time is subtracted from the value of this parameter, and if the result is greater than or equal to the value of parameter'delta', then the session is expired. Methodexpire_sessions()is the only method in this module which uses this parameter. The default value is obtained by callingtime(). This parameter is optional. β°verboseβ This is a integer,0or1, which β when set to1β causes progress messages to be written toSTDOUT. The default value is0. This parameter is optional. π
π METHOD: expire_db_sessions() π
Returns nothing.
This method uses the dbh parameter passed to new() to delete database-type sessions. bye boo π
πΈ METHOD: expire_file_sessions() πΈ
Returns nothing.
This method uses the temp_dir parameter passed to new() to delete file-type sessions. β¨
π METHOD: expire_sessions() π
Return value:
undefβ Returnsundefif your version ofCGI::Sessiondoes not support methodfind(). Also, returnsundefwhenCGI::Session's methodfind()failed for some reason. π1β Returns1whenfind()succeeds. π
Returns the result of calling CGI::Session's method find(), which will be undef for some sort of failure, and 1 for success.
This method handles both file-based and db-based sessions. multitasking queen π
π§ EXAMPLE CODE π§
See the examples/ directory in the distro.
There are 2 demo programs: expire-sessions.pl and expire-set.pl.
π«Ά SUPPORT π«Ά
Bugs should be reported via the CPAN bug tracker at
https://github.com/ronsavage/CGI-Session-ExpireSessions/issues
π¦ REPOSITORY π¦
https://github.com/ronsavage/CGI-Session-ExpireSessions.
β AUTHOR β
CGI::Session::ExpireSessions was written by Ron Savage <ron@savage.net.au> in 2004.
Home page: http://savage.net.au/index.html
π COPYRIGHT π
Australian copyright (c) 2004, Ron Savage. All Programs of mine are 'OSI Certified Open Source Software'; you can redistribute them and/or modify them under the terms of The Artistic License, a copy of which is available at:
http://www.opensource.org/licenses/index.html
and that's the whole page bestie, you ate that π β¨ this wasperl v5.32.1, dated2021-09-25, filed on the shelf asCGI::S...ssions(3pm)β go expire those sessions, ily π«Άπ
π¦ where this page comes from
- debian bookworm: libcgi-session-expiresessions-perl 1.14-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libcgi-session-expiresessions-perl 1.14-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libcgi-session-expiresessions-perl 1.14-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libcgi-session-expiresessions-perl 1.14-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libcgi-session-expiresessions-perl 1.14-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libcgi-session-expiresessions-perl 1.14-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms