I just implemented a custom session handler. A tough endeavor as guidance is rare and often inappropriate. Reading your code, I see numerous problems. You should run a syntax check to start with. To help you move faster, I contribute my code with the necessary advice. This code works. Well, as I removed a few peculiar things, it should work.:rolleyes:
Here is the code for you to examine:
<?php
/*
Set of functions to support PHP session handling using MySQL storage
Requires a preliminary connection to MySQL database named $connection
php.ini configuration file should have appropriate following settings:
session.auto_start = off ;
session.save_handler = user ; not files or mm
session.save_path = your_database_name ; enter appropriate name
session.name = your_table_name ; enter appropriate name
session.gc_maxlifetime = 1440 ; a number of seconds, here 24 minutes (24 * 60)
session.gc_probability = 10 ; integer percentage to trigger garbage removal
(make it 20 for testing, and 1 should suffice thereafter)
*/
$s_error_log = 'C:\\s_errors.log'; // any file name and location you like
function s_open($database_name, $table_name)
/*
Predefined PHP variable $database_name corresponds
to session.save_path in php.ini configuration file
Predefined PHP variable $table_name corresponds
to session.name in php.ini configuration file
*/
{
global $connection;
global $s_table;
$s_table = $table_name;
return TRUE;
};
function s_read($session_id)
// $session_id is a predefined PHP variable
{
global $connection;
global $s_table;
/*
MySQL table holding session data is made of three fields:
tag varchar(32) not null, primary key -- to hold the session identifier
payload text -- to hold the serialized string of session data
refresh timestamp -- to hold last access moment for garbage collection (pruning)
*/
$query = "SELECT * FROM $s_table WHERE tag = '$session_id'";
$result = mysql_query($query, $connection);
if (!$result)
die("Read error: ".mysql_errno()." : ".mysql_error()."\n");
if (mysql_num_rows($result) == 0) // No session found
return "";
else
{
$row = mysql_fetch_array($result);
return $row['payload'];
}
};
function s_write($session_id, $data)
// $session_id and $data are predefined PHP variables
{
global $connection;
global $s_table;
global $s_error_log;
$data = addslashes($data);
$query = "REPLACE $s_table (tag, payload) VALUES ('$session_id', '$data')";
mysql_query($query, $connection)
or error_log("Write error: "
.mysql_errno()." : ".mysql_error()."\n", 3, $s_error_log);
return TRUE;
};
function s_close()
{
global $connection;
mysql_close($connection);
return TRUE;
};
function s_zap($session_id)
// $session_id is a predefined PHP variable
{
global $connection;
global $s_table; // made global by s_open function
global $s_error_log;
$query = "DELETE FROM $s_table WHERE tag = '$session_id' ";
mysql_query($query, $connection)
or error_log("Destroy error: "
.mysql_errno()." : ". mysql_error()."\n", 3, $s_error_log);;
return TRUE;
};
function s_prune($max_time)
/*
Predefined PHP variable $max_time corresponds
to session.gc_maxlifetime in php.ini configuration file
Predefined PHP variable $table_name corresponds
to session.name in php.ini configuration file
Function is called according to probability percentage set by
session.gc_probability in php.ini configuration file
*/
{
global $connection;
global $s_table;
global $s_error_log;
$query = "DELETE FROM $s_table
WHERE UNIX_TIMESTAMP(refresh) < UNIX_TIMESTAMP() - $max_time";
mysql_query($query, $connection)
or error_log("Prune error: "
.mysql_errno()." : ". mysql_error()."\n", 3, $s_error_log);
return TRUE;
};
session_set_save_handler('s_open','s_close','s_read','s_write','s_zap','s_prune');
?>