include_once "adodb/session/adodb-session2.php";
ADOdb_Session::config($driver, $host, $user, $password, $database,$options=false);
session_start();
/*
* Test session vars, the following should increment on refresh
*/
if (!isset($_SESSION['AVAR']))
$_SESSION['AVAR'] = 0;
$_SESSION['AVAR'] += 1;
print "\$_SESSION['AVAR']={$_SESSION['AVAR']}
";
When the session is created in session_start( ), the global variable ''$ADODB_SESS_CONN'' holds the connection object.
The default name of the table is **sessions2**.
==== Overriding Table Name Defaults ====
include_once "adodb/session/adodb-session2.php";
$options['table'] = 'mytablename';
ADOdb_Session::config($driver, $host, $user, $password, $database,$options);
session_start();
===== ADOdb Session Handler Features =====
* Ability to define a notification function that is called when a session expires. Typically used to detect session logout and release global resources.
* Optimization of database writes. We crc32 the session data and only perform an update to the session data if there is a data change.
* Support for large amounts of session data with CLOBs (see adodb-session-clob2.php). Useful for Oracle.
* Support for encrypted session data, see adodb-cryptsession2.php. Enabling encryption is simply a matter of including adodb-cryptsession2.php instead of adodb-session2.php.
==== Session Handler Files ====
There are 3 session management files that you can use:
adodb-session2.php : The default
adodb-cryptsession2.php : Use this if you want to store encrypted
session data in the database
adodb-session-clob2.php : Use this if you are storing DATA in clobs
and you are NOT using oci8 driver
==== Non-persistent Connections====
To force non-persistent connections, call ''persist()'' before ''session_start()'':
include_once "adodb/session/adodb-session2.php";
$driver = 'mysql';
$host = 'localhost';
$user = 'auser';
$pwd = 'secret';
$database = 'sessiondb';
ADOdb_Session::config($driver, $host, $user, $password, $database, $options=false);
ADOdb_Session::persist(false);
session_start();
==== DSN Support ====
using DSN support so you can set other options such as port number:
include_once "adodb/session/adodb-session2.php";
$dsn = 'mysql://root:pwd@localhost/mydb?persist=1&port=5654';
ADOdb_Session::config($dsn, '', '', '');
session_start();
===== Using Encrypted Sessions =====
To use encrypted sessions, replace the file ''adodb-session2.php'' with ''adodb-cryptsession2.php'':
include 'adodb/session/adodb-cryptsession2.php';
$driver = 'mysql';
$host = 'localhost';
$user = 'auser';
$pwd = 'secret';
$database = 'sessiondb';
ADOdb_Session::config($driver, $host, $user, $password, $database,$options=false);
session_start();
And the same technique for adodb-session-clob2.php:
include 'adodb/session/adodb-session2-clob2.php';
$driver = 'oci8';
$host = 'localhost';
$user = 'auser';
$pwd = 'secret';
$database = 'sessiondb';
ADOdb_Session::config($driver, $host, $user, $password, $database,$options=false);
session_start();
===== Installation =====
Create this table in your database.
==== MySQL or PDO MySQL ====
CREATE TABLE sessions2 (
sesskey VARCHAR( 64 ) COLLATE utf8mb4_bin NOT NULL DEFAULT '',
expiry DATETIME NOT NULL ,
expireref VARCHAR( 250 ) DEFAULT '',
created DATETIME NOT NULL ,
modified DATETIME NOT NULL ,
sessdata LONGTEXT,
PRIMARY KEY ( sesskey ) ,
INDEX sess2_expiry( expiry ),
INDEX sess2_expireref( expireref )
)
$ADODB_SESSION_EXPIRE_NOTIFY = array('USERID','NotifyFn');
And when the NotifyFn is called (when the session expires), the ''$EXPIREREF'' holding the user id is passed in as the first parameter, e.g. ''NotifyFn($userid, $sesskey)''. The session key (which is the primary key of the record in the sessions table) is the 2nd parameter.
Here is an example of a Notification function that deletes some records in the database and temporary files:
function NotifyFn($expireref, $sesskey)
{
global $ADODB_SESS_CONN; # the session connection object
$user = $ADODB_SESS_CONN->qstr($expireref);
$ADODB_SESS_CONN->execute("delete from shopping_cart where user=$user");
system("rm /work/tmpfiles/$expireref/*");
}
$GLOBALS['USERID'] = GetUserID();
$ADODB_SESSION_EXPIRE_NOTIFY = array('USERID','NotifyFn');
In older versions of ADOdb this could be achieved automatically through the use of register_globals, but this feature has been [[https://php-legacy-docs.zend.com/manual/php5/en/security.globals|removed in PHP 5.4 for security reasons]].
ADODB_Session::filter(new ADODB_Compress_Bzip2());
ADODB_Session::filter(new ADODB_Encrypt_MD5());
will compress and then encrypt the record in the database.
===== Backwards Compatibility =====
The older method of connecting to ADOdb using global variables is now deprecated, and **will be removed** in ADOdb version 6.0:
$ADODB_SESSION_DRIVER='mysql';
$ADODB_SESSION_CONNECT='localhost';
$ADODB_SESSION_USER ='root';
$ADODB_SESSION_PWD ='abc';
$ADODB_SESSION_DB ='employees';
include 'adodb/adodb-session.php';