I'm just learning -- so any comments (did I just say that? **DUCK... INCOMING!!!
) Would be greatly appreciated..
Here is my Session Handler Class in it's entirety:
PHP Code:
<?php
class DBSessionHandler
{
protected $db_host,
$db_name,
$db_table,
$db_user,
$db_pass,
$db_pdo_dsn,
$db_handle,
$session_data = array() ;
public function __construct( $cleanDB )
{
$this->db_host = $cleanDB['host'] ;
$this->db_user = $cleanDB['username'] ;
$this->db_pass = $cleanDB['password'] ;
$this->db_name = $cleanDB['database'] ;
$this->db_table = $cleanDB['table'] ;
$this->db_pdo_dsn = "mysql:host=$this->db_host;dbname=$this->db_name" ;
}
public function sessionSetSaveHandler( )
{
session_set_save_handler(
array($this, 'sessionOpen'),
array($this, 'sessionClose'),
array($this, 'sessionRead'),
array($this, 'sessionWrite'),
array($this, 'sessionDestroy'),
array($this, 'sessionClean') ) ;
}
public function sessionOpen( )
{
return true;
}
public function sessionClose( )
{
try
{
$this->db_handle = null ;
}
catch ( Exception $e )
{
error_log( "SESSION CLOSE Error message = {$e->getMessage()} " ) ;
return false ;
}
return true;
}
public function sessionRead( $id )
{
$sql = "SELECT `session_data` FROM {$this->db_table} WHERE `session_id` = '$id'" ;
$this->db_handle = new PDO($this->db_pdo_dsn, $this->db_user, $this->db_pass ) ;
$statement = $this->db_handle->query( $sql ) ;
$result = $statement->fetch( PDO::FETCH_ASSOC ) ;
try
{
if( count ( $result ) > 0 )
{
$this->set_session_data('id', $result['session_id'] ) ;
return $result['session_data'] ;
}
else
{
throw new Exception( "No record found in DB with that id ( {$id} )" ) ;
}
}
catch ( Exception $e )
{
error_log( "Error message = {$e->getMessage()} " ) ;
return '' ;
}
}
public function sessionWrite( $id, $data)
{
try
{
$this->db_handle = new PDO($this->db_pdo_dsn, $this->db_user, $this->db_pass ) ;
$time = time() ;
$sql = "SELECT * FROM {$this->db_table} WHERE session_id = '" . session_id() . "'" ;
$statement = $this->db_handle->query( $sql ) ;
$result = $statement->fetch( PDO::FETCH_ASSOC ) ;
if( !empty( $result ) )
{
/* a record exists and we need to UPDATE the record */
$sql = "UPDATE {$this->db_table} SET session_access = '$time', " .
"session_data = '{$data}' WHERE session_id = '{$id}'" ;
}
else
{
/* NO record exists and we need to do an INSERT */
$sql = "INSERT INTO {$this->db_table} (" .
"session_id, session_start, session_access, session_data) " .
"VALUES ('$id', '$time', '$time', '$data') " ;
}
$result = $this->db_handle->exec($sql) ;
return $result ;
}
catch ( Exception $e )
{
error_log('EXITING the DBSessionHandler::sessionWrite() METHOD WITH ERRORS! ') ;
error_log( ' >> Error message = ' . $e->getMessage()) ;
error_log(' >> Line Number: ' . $e->getLine( ) . ' In file: ' . $e->getFile( ) .'\n') ;
}
}
public function sessionDestroy( $id )
{
$sql = "DELETE FROM {$this->db_table} WHERE session_id = '{$id}'" ;
try
{
$count = $this->db_handle->exec($sql) ;
if( $count > 0 )
{
return true;
}
}
catch ( Exception $e )
{
return false ;
}
}
public function sessionClean( $max_life )
{
$old_sessions = time() - $max_life ;
$sql = "DELETE FROM {$this->db_table} WHERE session_access < '{$old_sessions}'" ;
try
{
$count = $this->db_handle->exec($sql) ;
if( $count > 0 )
{
return true ;
}
}
catch ( Exception $e )
{
return false ;
}
}
public function set_session_data( $key, $value)
{
$this->session_data[$key] = $value ;
}
public function get_session_data( $key )
{
return $this->session_data[$key] ;
}
public function __destruct( )
{
session_write_close() ;
}
}
?>
THE "dbConxn.php" file contains (note this is on my dev server here at home):
PHP Code:
$cleanDB = array( ) ;
$cleanDB['host'] = "localhost" ;
$cleanDB['username'] = "root" ;
$cleanDB['password'] = "root" ;
And the "test" file that "tries" to use it:
PHP Code:
require_once "application/controllers/dbConxn.php" ;
require_once "session/DBSessionHandler.Class.php" ;
$cleanDB['database'] = "admin" ;
$cleanDB['table'] = "session" ;
$objMySession = new DBSessionHandler( $cleanDB ) ;
$objMySession->sessionSetSaveHandler() ;
session_start() ;
if(isset($_SESSION['views']))
{
$_SESSION['views']=$_SESSION['views']+1;
}
else
{
$_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
}
and.. just in case... this is the MySQL table:
Code:
CREATE TABLE IF NOT EXISTS `session` (
`session_id` varchar(64) NOT NULL,
`session_start` int(10) NOT NULL,
`session_access` int(10) unsigned DEFAULT NULL,
`session_data` text,
PRIMARY KEY (`session_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Bookmarks