As I have said above.. the rest of it works.. and i gave an example of where i used the code and it not working.. which is the exact code from my site.. but if you wish to see the rest of my session handler:
common.php:
$sessionLifetime = get_cfg_var("session.gc_maxlifetime");
function sess_regen_id()
{
$old = session_id();
session_regenerate_id();
sess_destroy($old);
}
function sess_open($who, $cares)
{
return true;
}
function sess_close()
{
return true;
}
function sess_read($sess_id)
{
global $DB;
$sql = "SELECT `session_data` FROM `sessions` WHERE
session_id = '".$sess_id."' AND
session_agent = '".$_SERVER["HTTP_USER_AGENT"]."' AND
session_expire > '".time()."';";
$query = $DB->query($sql);
if($DB->num_rows($query) > 0)
{
$r = $DB->fetch($query);
return $r['session_data'];
}
return '';
}
function sess_write($sess_id, $sess_data)
{
global $DB, $sessionLifetime;
$query = $DB->query("SELECT * FROM `sessions` WHERE `session_id` = '". $sess_id ."';");
if($DB->num_rows($query) > 0)
{
$DB->query("UPDATE `sessions` SET `session_data` = '". $sess_data ."',
`session_expire` = '". (time() + $sessionLifetime) ."' WHERE `session_id` = '". $sess_id ."';");
if($DB->affected() > 0)
return true;
return false;
}
else
{
$DB->query("INSERT INTO `sessions`(`session_id`, `session_data`, `session_expire`, `session_agent`, `session_ip`) VALUES
('". $sess_id ."', '". $sess_data ."', '". (time() + $sessionLifetime) ."', '". $_SERVER['HTTP_USER_AGENT'] ."', '". $_SERVER['REMOTE_ADDR'] ."');");
if($DB->affected() > 0)
return '';
return false;
}
return false;
}
function sess_destroy($sess_id)
{
global $DB;
$DB->query("DELETE FROM `sessions` WHERE `session_id` = '". $sess_id ."';");
if($DB->affected() > 0)
return true;
return false;
}
function sess_gc($limit)
{
global $DB;
$DB->query("DELETE FROM `sessions` WHERE `session_expire` > '". (time() - $limit) ."';");
if($DB->affected() > 0)
return true;
return false;
}
session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc");
register_shutdown_function('session_write_close');
session_start();
the database class i am using i know works.. and in this scope it works as again it works with the other functions.. also again as i said above no errors are thrown. (before you say i don't check for errors on the queries my DB class handles that).
the $_SESSION should be global by default.. and simply commenting all of the above code out works with the default PHP session handler so it's not a scope problem for $_SESSION (which is used directly after this file is included anyway)
i will include the code i used again to test all of this:
index.php
require_once './common.php';
var_dump($_SESSION); // outputs array(0) {}
if(!isset($_SESSION['count']))
$_SESSION['count'] = 0;
else
$_SESSION['count']++;
echo $_SESSION['count']; // outputs 0 everytime
// rest of code.
since session_start() is in common.php it is running before the sessions are being called.. and the auto start is not on for PHP sessions.