I am having 2 problems -- probably related -- with the Sessions class I am writing (please see below):
-
A new session id is generated every time the page changes
-
As far as I can tell, the session cookie is never generated
I should add that the session data is being written to the database correctly.
Any thoughts on what I have overlooked that would cause this?
Many thanks!
<?php
// CREATE TABLE IF NOT EXISTS SessionTable
// (
// session_id varchar(32) collate utf8_bin NOT NULL default '',
// session_ts timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
// session_data text collate utf8_bin,
// PRIMARY KEY (session_id)
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Session Data';
class Session
{
// Properties
protected $sessionTimeout = NULL;
protected $dbHandle = NULL;
protected $sessionDbTable = NULL;
protected $sessionName = NULL;
protected $sessionId = NULL;
// Methods
public function __construct
(
$sessionTimeout = 1800,
$dbh = NULL,
$sessionDbTable = NULL,
$sessionName = NULL
)
{
ini_set('session.gc_maxlifetime', $sessionTimeout);
$this->sessionTimeout = $sessionTimeout;
if ( ( ! is_null( $dbh ) ) && ( is_object( $dbh ) ) )
{
$this->dbHandle = $dbh;
}
if ( ( ! is_null( $sessionDbTable ) ) &&
( is_string( $sessionDbTable ) ) )
{
$this->sessionDbTable = $sessionDbTable;
}
if ( ( ! is_null( $sessionName ) ) &&
( is_string( $sessionName ) ) )
{
ini_set( "session.name", $sessionName );
$this->sessionName = $sessionName;
}
session_set_save_handler
(
array(&$this, 'open'),
array(&$this, 'close'),
array(&$this, 'read'),
array(&$this, 'write'),
array(&$this, 'destroy'),
array(&$this, 'gc')
);
register_shutdown_function('session_write_close');
ini_set('session.use_cookies', 1);
session_set_cookie_params
(
$this->sessionTimeout, // Length of session
'/', // Cookie's domain
$_SERVER[ 'SERVER_NAME' ] . '/arsfabulae.com', // Server the session is coming from
0, // Is the session secure?
1 // Is the session HTTP only?
);
error_log("Cookie Parms: " . print_r(session_get_cookie_params(),1));
session_start();
$this->setSessionId();
}
public function open( $path, $name ) { return is_object( $this->dbHandle ); }
public function close() { return TRUE; }
public function read( $session_id )
{
$sql = "SELECT session_data FROM " . $this->sessionDbTable . ' ' .
"WHERE session_id = '$session_id' " .
"AND (UNIX_TIMESTAMP() - UNIX_TIMESTAMP( session_ts) ) <= " .
$this->sessionTimeout;
$req = $this->dbHandle->query( $sql );
if ( PEAR::isError( $req ) ) { throw new DbException( $req ); }
else
{
$row = $req->fetchRow();
return $row[ 'session_data' ];
}
}
public function write( $session_id, $session_data )
{
$sql = "REPLACE INTO " . $this->sessionDbTable . ' ' .
"VALUES('$session_id', NOW(), '$session_data') ";
$req = $this->dbHandle->query( $sql );
if ( PEAR::isError( $req ) ) { throw new DbException( $req ); }
return TRUE;
}
public function destroy( $session_id )
{
$sql = "DELETE FROM " . $this->sessionDbTable . ' ' .
"WHERE session_id = '$session_id'";
$req = $this->dbHandle->query( $sql );
if ( PEAR::isError( $req ) ) { throw new DbException( $req ); }
return TRUE;
}
public function gc ( $sessionTimeout )
{
$sql = "DELETE FROM " . $this->sessionDbTable . ' ' .
"WHERE (UNIX_TIMESTAMP() - UNIX_TIMESTAMP( session_ts) ) > " .
$this->sessionTimeout;
$req = $this->dbHandle->query( $sql );
if ( PEAR::isError( $req ) ) { throw new DbException( $req ); }
return TRUE;
}
public function getSessionName() { return $this->sessionName; }
protected function setSessionId()
{
$this->sessionId = session_id();
}
public function getSessionId() { return $this->sessionId; }
public function setSessionVariable( $name, $value )
{
$_SESSION[ $name ] = $value;
return TRUE;
}
public function getSessionValue( $name ) { return $_SESSION[ $name ]; }
}
?>