I'm trying to write a custom session persister in PHP. But for some reason only my session read functon is getting called, not my session write function. Thus the session isn't persisted. If I switch back to the default session handler (file), everything works. If I add session_write_close()
at the end of the test script - everything works. But PHP manual explicitly states that you do not need to explicitly call the session_write_close()
because it gets automatically called at the end of the script. And I don't want to put session_write_close() at every possible exit point in my existing application!
Here's some code that illustrates the problem. I'm running it on WinXP SP3 + Apache 2.2.9 + PHP 5.2.6. The log file only contains entries for s_open()
and s_read()
.
<?php
function logger($str)
{
file_put_contents('session_log.txt', date('Y-m-d h:i:s') . " $str\r\n", FILE_APPEND);
}
function s_open()
{
logger('s_open');
return true;
}
function s_close()
{
logger('s_close');
return true;
}
function s_read()
{
logger('s_read');
return @file_get_contents('session.txt');
}
function s_write($id, $data)
{
logger("s_write: $id - $data");
file_put_contents('session.txt', $data);
return true;
}
function s_destroy()
{
logger('s_destroy');
return true;
}
function s_gc()
{
logger('s_gc');
return true;
}
session_set_save_handler('s_open', 's_close', 's_read', 's_write', 's_destroy', 's_gc');
session_start();
if ( isset($_SESSION['time']) )
echo 'Session time: ' . date('Y-m-d h:i:s', $_SESSION['time']);
else
echo 'Session empty';
echo '<br>';
echo 'Session ID: ' . session_id();
$_SESSION['time'] = time();
?>