Question

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();
?>
Was it helpful?

Solution

Blargh! Got caught on a notice that PHP itself had put there! To quote the manual:

Warning

Current working directory is changed with some SAPIs if session is closed in the script termination. It is possible to close the session earlier with session_write_close().

It was indeed changing and the calls to s_write() and s_close() were logged in another directory!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top