Hi all.
I'm using php 4.0.6, and working on a hacked-up version of a postgresql backed user-defined session script that Ifound on the web somewhere.
In previous (4.0.4) versions of php, the scripts works fine. When I call, say, session_register("var"), I get an entry for "var" in the postgresql session db. But with 4.0.6, vars NEVER get written to the db.
Does anyone know of any bugs with user-defined session scripts in php 4.0.6?
here's the session script, by the way:
<?php
/**
PostgreSQL Session Handler for PHP
Requirements
* - In php.ini, set session.save_handler to 'user'.
* - In php.ini, set session.save_path to the name of the database table.
* - Create the table structure using the follow schema:
*
* CREATE TABLE php_sessions (
* id CHAR(64) NOT NULL PRIMARY KEY,
* counter INTEGER NOT NULL,
* data TEXT
* );
*
*/
/* Include the top-level config directives */
require_once("system_settings");
/* Get the name of the session table. Default to 'php_sessions'. */
$pgsql_session_table = $GLOBALS['session_table'];
/* Global PostgreSQL database connection handle. */
$pgsql_session_handle = 0;
/**
* Opens a new session.
*
* @param $save_path The value of session.save_path.
* @param $session_name The name of the session ('PHPSESSID').
*
* @return boolean True on success, false on failure.
*/
function pgsql_session_open($save_path, $session_name)
{
global $pgsql_session_handle;
$params = "host=" . $GLOBALS['session_dbhost'];
$params .= " dbname=" . $GLOBALS['session_db'];
$params .= " user=" . $GLOBALS['dbuser'];
$params .= " password=" . $GLOBALS['dbpass'];
// error_log($params,0);
$pgsql_session_handle = pg_connect($params);
// error_log("handle= $pgsql_session_handle",0);
return ($pgsql_session_handle != 0);
}
/**
* Closes the current session.
*
* @return boolean True on success, false on failure.
*/
function pgsql_session_close()
{
global $pgsql_session_handle;
if (isset($pgsql_session_handle) && $pgsql_session_handle !=0) {
return pg_close($pgsql_session_handle);
}
return true;
}
/**
* Reads the requested session data from the database.
*
* @param $key Unique session ID of the requested entry.
*
* @return string The requested session data, or false on failure.
*/
function pgsql_session_read($key)
{
global $pgsql_session_handle, $pgsql_session_table;
// error_log("handle= $pgsql_session_handle",0);
$key = addslashes($key);
// error_log("read-key = $key",0);
/* Build and execute the database query. */
$query = "SELECT data FROM $pgsql_session_table WHERE id = '$key'";
$result = pg_exec($pgsql_session_handle, $query);
if (isset($result) && $result && (pg_numrows($result) == 1)) {
$data = pg_fetch_row($result, 0);
pg_freeresult($result);
// error_log("read-data = $data",0);
return $data;
} else {
return false;
}
}/**
* Writes the provided session data with the requested key to the database.
*
* @param $key Unique session ID of the current entry.
* @param $val String containing the session data.
*
* @return boolean True on success, false on failure.
*/
function pgsql_session_write($key, $val)
{
global $pgsql_session_handle, $pgsql_session_table;
// error_log("handle = $pgsql_session_handle\n",0);
$key = addslashes($key);
$val = addslashes($val);
// error_log("write-key = $key\nwrite-val = $val",0);
$now = time();
/*
* Delete any existing data for this session and then insert a new row
* containing the current session data, all in a single transaction.
* This should prevent collisions between multiple session instances.
*/
$query = 'BEGIN';
pg_exec($pgsql_session_handle, $query);
$query = "DELETE FROM $pgsql_session_table WHERE id = '$key'";
pg_exec($pgsql_session_handle, $query);
$query = "INSERT INTO $pgsql_session_table values('$key', $now, '$val')";
$result = pg_exec($pgsql_session_handle, $query);
$ret = (pg_cmdtuples($result) != 0);
pg_freeresult($result);
$query = 'COMMIT';
pg_exec($pgsql_session_handle, $query);
return ($ret);
}
/**
* Destroys the requested session.
*
* @param $key Unique session ID of the requested entry.
*
* @return boolean True on success, false on failure.
*/
function pgsql_session_destroy($key)
{
global $pgsql_session_handle, $pgsql_session_table;
$key = addslashes($key);
$query = "DELETE FROM $pgsql_session_table WHERE id = '$key'";
$result = pg_exec($pgsql_session_handle, $query);
$ret = ($result != 0);
pg_freeresult($result);
return $ret;
}/**
* Performs session garbage collection based on the provided lifetime.
*
* @param $maxlifetime Maximum lifetime of a session.
*
* @return boolean True on success, false on failure.
*/
function pgsql_session_gc($maxlifetime)
{
global $pgsql_session_handle, $pgsql_session_table;
$expiry = time() - $maxlifetime;
$query = "DELETE FROM $pgsql_session_table WHERE counter < $expiry";
$result = pg_exec($pgsql_session_handle, $query);
$ret = ($result != 0);
pg_freeresult($result);
return $ret;
}
/* Register the session handling functions with PHP. */
session_set_save_handler(
"pgsql_session_open",
"pgsql_session_close",
"pgsql_session_read",
"pgsql_session_write",
"pgsql_session_destroy",
"pgsql_session_gc"
);
?>