I am testing user-handling of sessions (mysql database) and just do not understand
this behavior. my SESSION table is created as
CREATE TABLE SESSION (
id varchar(32) NOT NULL default '',
time timestamp(14) NOT NULL,
user_id mediumint(8) NOT NULL default '-1',
data text NOT NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;
I have a main script index.php3
?php
require(./sessions.inc);
session_module_name(user);
session_set_save_handler(sess_open, sess_close,
sess_read, sess_write, sess_remove, sess_gc);
session_start();
if (!isset($_SESSION[''])) {
$_SESSION[''] = 1;
print(br set );
}
function increment() {
$_SESSION['']++;
echo br=.$_SESSION[''].br;
}
function set_() {
echo set br;
$_SESSION[''] = 666;
}
if (isset($_POST['MYVAR']))
set_();
else
increment();
echo a href=\$PHP_SELF\$PHP_SELF increment /Abr;
echo a href=\$PHP_SELF?MYVAR=3\$PHP_SELF register /A;
?
and sessions.inc:
?php
function session_table() {
return(SESSION);
}
function session_log($message) {
if($file = fopen(/tmp/session.txt, a)) {
fwrite($file, date(Y-m-d H:i:s ) . $message . \n);
fclose($file);
}
}
function sess_open($path, $name) {
session_log(session_open);
return(true);
}
function sess_close() {
session_log(session_close);
return(true);
}
function sess_read($id) {
session_log(session_read);
$db = mysql_pconnect('localhost','mysqluser','password');
if(!mysql_select_db('rfox_auth')) {
session_log(session_read select database error:
. mysql_error());
return();
}
$sql = select * from . session_table()
. where id=' . $id . ';
if(!$result = mysql_query($sql)) {
session_log(MySQL error: . mysql_error()
. with SQL: . $sql);
return();
}
if(mysql_num_rows($result)) {
session_log(MySQL query returned . mysql_num_rows($result)
. rows.);
$row = mysql_fetch_assoc($result);
session_log(session_read returned . $row[data]);
return($row[data]);
}
else {
session_log(session_read found zero rows with SQL: . $sql);
return();
}
}
function sess_write($id, $data) {
session_log(session_write);
if(!mysql_select_db('rfox_auth')) {
session_log(session_write select database error:
. mysql_error());
return(false);
}
$sql = update . session_table()
. set data = ' . addslashes($data) . ', time = null;
$sql .= where id=' . $id . ';
if (!$result = mysql_query($sql)) {
session_log(session_write error . mysql_error()
. with SQL: . $sql);
return(false);
}
if (mysql_affected_rows()) {
session_log(session_write update affected
. mysql_affected_rows() . rows with SQL: . $sql);
return(true);
}
session_log(session_write updated zero rows with SQL: . $sql);
$sql = insert . session_table()
. set data = ' . addslashes($data) . ', id = ' . $id . ';
if(!$result = mysql_query($sql)) {
session_log(session_write error . mysql_error()
. with SQL: . $sql);
return(false);
}
else {
session_log(session_write inserted with SQL: . $sql);
return(true);
}
}
function sess_remove($id)
{
session_log(session_remove);
if(!mysql_select_db('rfox_auth'))
{
session_log(session_remove select database error:
. mysql_error());
return(false);
}
$sql = delete . session_table() . where id=' . $id . ';
if ($result = mysql_query($sql))
{
session_log(MySQL query delete worked.);
return(true);
}
else
{
session_log(MySQL update error: . mysql_error()
. with SQL: . $sql);
return(false);
}
}
function sess_gc($life)
{
session_log(session_gc);
if (!mysql_select_db('rfox_auth')) {
session_log(session_gc select database error: . mysql_error());
return(false);
}
$sql = delete from . session_table() . where time '
. date(YmdHis, time() - $life) . ';
session_log(session_gc sql: . $sql);
if ($result = mysql_query($sql)) {
session_log(session_gc deleted . mysql_affected_rows()
. rows.);
return(true);
}
else {
session_log(session_gc error: . mysql_error()
. with SQL: . $sql);
return(false);
}
}
?
I can see in the log file that session_write() is executed when I register . But
there is a problem:
PROBLEM: When I register , there is no call to session_write() in the log file,
and no data for gets written to the database.
Can you duplicate this behavior on your system? Do you have an explanation?
Thanks!
Rich