[PHP-DEV] PHP 4.0 Bug #9207 Updated: User session handler doesn't call write function From: jensenta2001 <email protected>
Date: 02/28/01

ID: 9207
User Update by: jensenta2001 <email protected>
Old-Status: Feedback
Status: Closed
Bug Type: *Session related
Description: User session handler doesn't call write function

Sorry for the inconvience the problem was in my code.

Previous Comments:
---------------------------------------------------------------------------

[2001-02-20 13:47:03] sas <email protected>
How did you verify that the write handler is not called? Note that during the request shutdown the write/close handlers cannot send any data to the browser.

---------------------------------------------------------------------------

[2001-02-10 16:01:43] jensenta2001 <email protected>
I'm trying to setup a user session module using a postgre (6.2.3) database. the script that I'm supplying defines the functions for storing session data. If I force the session data into the database the read function works and I can retrieve the session data. However, the write function never gets called so data is never stored in the database. This is simular to problems reported in #8772 and #9002. There is a difference though. Those reports indicate that register_globals was off. In my case register_globals was on. Here is the script I'm using to test the session module.

============================================
<?php

$sess_pg = array();
$sess_pg["open_connection"] = true; // Establish a Pg connection on session startup?
$sess_pg["hostname"] = "localhost"; // Pg hostname
$sess_pg["port"] = "5432"; // Pg port
$sess_pg["db"] = "php_sessions"; // Database where to store the sessions
$sess_pg["table"] = "sessions"; // Table where to store the sessions

function sess_pg_open($save_path, $sess_name)
{
    global $sess_pg;

    // Establish a Pg connection, if $sess_pg["open_connection"] is true
    if ($sess_pg["open_connection"])
    {
        $sess_pg["connection"] = pg_pconnect( $sess_pg["hostname"],
                                              $sess_pg["port"],
                                              $sess_pg["db"]) or die(pg_errormessage($sess_pg["connection"]));
    }

    return(true);
}

function sess_pg_read($sess_id)
{
    global $sess_pg;

        echo "HELP ME<br>";
        $query = "SELECT data ";
        $query .= "FROM ".$sess_pg["table"]." ";
        $query .= "WHERE id = '$sess_id'";
        echo "$query<BR>";

    // Select the data belonging to session $sess_id from the Pg session table
    $result = pg_exec($sess_pg["connection"], $query) or die(pg_errormessage($sess_pg["connection"])) or die(pg_errormessage($sess_pg["connection"]));
    #

    // Return an empty string if no data was found for this session
    //if(pg_numrows($result) == 0)
    if(!($sess_pg["present"] = pg_numrows($result)))
    {
        echo "Nothing<BR>";
        return("");
    }

    echo "Records: ".$sess_pg["present"]."<BR>";

    // Session data was found, so fetch and return it
    $row_number = 0;
    $row = pg_fetch_array($result,$row_number);
    pg_freeresult($result);

    echo "DATA: ".$row["data"]."<BR>";

    return($row["data"]);
}

function sess_pg_write($sess_id, $val)
{
    global $sess_pg;

    if(!$sess_pg["present"]){
        // Write the serialized session data ($val) to the Pg ession table
        $query = "INSERT INTO ". $sess_pg["table"] ." (id, data, t_stamp) ";
        $query .= "VALUES ($sess_id, $val, now())";
        $result = pg_exec($sess_pg["connection"], $query) or die(pg_errormessage($sess_pg["connection"]));
    }else{
        // Write the serialized session data ($val) to the Pg ession table
        $query ="UPDATE ".$sess_pg["table"];
        $query .=" SET data = '$val', t_stamp = now() ";
        $query .=" where id = '$sess_id'";
        $result = pg_exec($sess_pg["connection"], $query) or die(pg_errormessage($sess_pg["connection"]));
    }
    return(true);
}

function sess_pg_destroy($sess_id)
{
    global $sess_pg;

    // Delete from the Pg table all data for the session $sess_id
    $query ="DELETE FROM ".$sess_pg["table"]." ";
    $query .="WHERE id = '$sess_id' ";
    $result = pg_db_query($sess_pg["connection"], $query) or die(pg_errormessage($sess_pg["connection"]));
        
    return(true);
}

function sess_pg_gc($max_lifetime)
{
    global $sess_pg;

    // Old values are values with a Unix less than now - $max_lifetime
    $old = time() - $max_lifetime;

    // Delete old values from the Pg session table
    $result = pg_db_query($sess_pg["connection"], "DELETE FROM ".$sess_pg["table"]." WHERE UNIX_TIMESTAMP(t_stamp) < $old") or die(pg_errormessage($sess_pg["connection"]));
        
    return(true);
}

$foo = 10;
session_set_save_handler("sess_pg_open", "", "sess_pg_read", "sess_pg_write", "sess_pg_destroy", "sess_pg_gc");
session_start();
session_register("foo");
echo "foo: $foo";
$foo++;
?>

---------------------------------------------------------------------------

Full Bug description available at: http://bugs.php.net/?id=9207

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: php-dev-unsubscribe <email protected>
For additional commands, e-mail: php-dev-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>