Here is the complete code from the class... it's just a modified version of
that available from an article on the Zend site
<?
$ses_class = new session();
ini_set("session.save_handler", "user");
ini_set('session.gc_maxlifetime', SESSION_LIFETIME * 60);
/* Change the save_handler to use the class functions */
session_set_save_handler (array(&$ses_class, '_open'),
array(&$ses_class, '_close'),
array(&$ses_class, '_read'),
array(&$ses_class, '_write'),
array(&$ses_class, '_destroy'),
array(&$ses_class, '_gc'));
/* Start the session */
session_start();
class session
{
/* Define the mysql table you wish to use with
this class, this table MUST exist. */
var $ses_table = "";
/* Configure the info to connect to MySQL, only required
if $db_con is set to 'Y' */
var $db_host = "";
var $db_user = "";
var $db_pass = "";
var $db_dbase = "";
var $dblink;
function session()
{ // perform variable assignments
$this->ses_table = SESSION_TABLE;
$this->db_host = DB_HOST;
$this->db_user = DB_USER;
$this->db_pass = DB_PASS;
$this->db_dbase = DB_DB_NAME;
}
function localError($code = '', $message = '', $file = '', $line =
-1, $status = ERROR_STATUS_NOTIFY)
{ // handle a local error
global $base;
if (class_exists('baseClass'))
{ // use the base class error handler
$base->errorPush($code, $message, $file, $line,
$status);
} else {
die ('<b>Error ' . $code . '</b><br/>' . $message);
}
}
/* Create a connection to a database */
function db_connect() {
$this->dblink = @mysql_pconnect ($this->db_host, $this->db_user,
$this->db_pass);
if (!$this->dblink) $this->localError('c1(ses)',
mysql_error(), __file__, __line__, ERROR_STATUS_FATAL);
$mysql_db = @mysql_select_db ($this->db_dbase);
if (!$mysql_db) $this->localError('c1(ses)', mysql_error() .
' - Tried: ' . $this->db_dbase, __file__, __line__, ERROR_STATUS_FATAL);
if (!$this->dblink || !$mysql_db) {
return FALSE;
} else {
return TRUE;
}
}
/* Open session, if you have your own db connection
code, put it in here! */
function _open($path, $name) {
$this->db_connect();
return TRUE;
}
/* Close session */
function _close() {
/* This is used for a manual call of the
session gc function */
$this->_gc(0);
return TRUE;
}
/* Read session data from database */
function _read($ses_id) {
$session_sql = "SELECT * FROM " . $this->ses_table
. " WHERE ses_id = '$ses_id'";
$session_res = @mysql_query($session_sql, $this->dblink) or die
(mysql_error());
if (!$session_res) {
return '';
}
$session_num = @mysql_num_rows ($session_res);
if ($session_num > 0) {
$session_row = mysql_fetch_assoc ($session_res);
$ses_data = $session_row["ses_value"];
return $ses_data;
} else {
return '';
}
}
/* Write new data to database */
function _write($ses_id, $data) {
$session_sql = "UPDATE " . $this->ses_table
. " SET ses_time='" . time()
. "', ses_value='$data' WHERE ses_id='$ses_id'";
$session_res = @mysql_query ($session_sql, $this->dblink) or die
(mysql_error());
if (!$session_res) {
return FALSE;
}
if (mysql_affected_rows ()) {
return TRUE;
}
$session_sql = "INSERT INTO " . $this->ses_table
. " (ses_id, ses_time, ses_start, ses_value)"
. " VALUES ('$ses_id', '" . time()
. "', '" . time() . "', '$data')";
$session_res = @mysql_query ($session_sql, $this->dblink) or die
(mysql_error());
if (!$session_res) {
return FALSE;
} else {
return TRUE;
}
}
/* Destroy session record in database */
function _destroy($ses_id) {
$session_sql = "DELETE FROM " . $this->ses_table
. " WHERE ses_id = '$ses_id'";
$session_res = @mysql_query ($session_sql, $this->dblink) or die
(mysql_error());
if (!$session_res) {
return FALSE;
} else {
return TRUE;
}
}
/* Garbage collection, deletes old sessions */
function _gc($life) {
$ses_life = strtotime("-" . SESSION_LIFETIME . " minutes");
$session_sql = "DELETE FROM " . $this->ses_table
. " WHERE ses_time < $ses_life";
$session_res = @mysql_query ($session_sql, $this->dblink);
if (!$session_res) $this->localError('c1(ses)',
mysql_error() . ' - SQL: ' . $session_sql . '<br/><a href="' . WS_SITE_ROOT
. '?action=build">Click here to build session tables (for database)</a>',
__file__, __line__, ERROR_STATUS_FATAL);
if (!$session_res) {
return FALSE;
} else {
return TRUE;
}
}
}
?>
-----Original Message-----
From: Peter Lauri [mailto:[EMAIL PROTECTED]
Sent: 16 February 2006 12:25
To: [EMAIL PROTECTED]; [email protected]
Subject: RE: [PHP] Session problems
What is the code that generates this?
-----Original Message-----
From: "Dan Parry"<[EMAIL PROTECTED]>
Sent: 16/02/06 4:50:51 PM
To: "[email protected]"<[email protected]>
Subject: [PHP] Session problems
Hi all
I've been getting an error while using a custom session handler
utilising a
MySQL database
The error I receive is:
Duplicate entry '<<PHPSESSID_REMOVED>>' for key 1
Warning: Unknown(): A session is active. You cannot change the session
module's ini settings at this time. in Unknown on line 0
I've contacted my hosting company regarding this but currently they
don't
seem to know the cause
I've Googled but I can't find anything I can use
Thanks in advance :-)
Dan
-----------------------------------------------------
Dan Parry
Senior Developer
Virtua Webtech Ltd
http://www.virtuawebtech.co.uk
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php