Hello,
I am trying to get session data written to a MySQL table using
set_session_save_handler(), but am getting the following error when I
issue session_register(varname) on a page:
Fatal error: Failed to initialize session module in
/Library/WebServer/Documents/VirtualHosts/www/admin/en/login.php on
line 4
I have set session.save_handler to user and session.save_path to
mysql://USER:PASS@localhost/DB in my php.ini file. File-based
sessions seem to work fine with session.save_handler set to files and
session.save_path to /tmp. I am using the following code as my save
handler (taken from Web Application Development With PHP 4.0 by
Tobias Ratschiller and Till Gerken):
?php
$sess_mysql = array();
$sess_mysql[open_connection] = true;
$sess_mysql[hostname] = localhost;
$sess_mysql[user] = USER;
$sess_mysql[password] = PASS;
$sess_mysql[db] = DB;
$sess_mysql[table] = sessions;
function sess_mysql_open($save_path, $sess_name)
{
global $sess_mysql;
if ($sess_mysql[open_connection])
{
$link = mysql_pconnect($sess_mysql[hostname],
$sess_mysql[user],
$sess_mysql[password]) or die(mysql_error());
}
return (true);
}
function sess_mysql_read($sess_id)
{
global $sess_mysql;
$result = mysql_db_query($sess_mysql[db], SELECT data FROM
.$sess_mysql[table] . WHERE id =
'$sess_id') or
die(mysql_error());
if (mysql_num_rows($result) == 0)
{
return ();
}
$row = mysql_fetch_array($result);
mysql_free_result($result);
return ($row[data]);
}
function sess_mysql_write($sess_id, $val)
{
global $sess_mysql;
$result = mysql_db_query($sess_mysql[db], REPLACE INTO
.$sess_mysql[table] . VALUES
('$sess_id', '$val', null)) or
die(mysql_error());
return (true);
}
function sess_mysql_destroy($sess_id)
{
global $sess_mysql;
$result = mysql_db_query($sess_mysql[db], DELETE FROM
.$sess_mysql[table] . WHERE id =
'$sess_id') or
die(mysql_error());
return (true);
}
function sess_mysql_gc($max_lifetime)
{
global $sess_mysql;
$old = time() - $max_lifetime;
$result = mysql_db_query($sess_mysql[db], DELETE FROM
.$sess_mysql[table] . WHERE
UNIX_TIMESTAMP(t_stamp) $old) or
die(mysql_error());
return (true);
}
session_set_save_handler(sess_mysql_open, , sess_mysql_read,
sess_mysql_write,
sess_mysql_destroy, sess_mysql_gc);
?
I have also tried Sam Johnston's PEAR Custom Session Handler for PHP4
(http://sourceforge.net/projects/pearsession/), but I get the same
error. Like I said, file-based sessions are working fine, but any
custom handler bombs with the error I noted above. This is all
running under Mac OS X 10.1.1, PHP 4.0.6 using the PEAR DB libraries
(among others) and MySQL 3.23.43.
What could be causing the error? I've searched and searched but
can't seem to find any answers. Thanks in advance,
-f
--
Derek Fong
Web Application Developer
subtitle designs inc. http://www.subtitled.com/
Mistakes are the portals of discovery. --James Joyce
GPG key/fingerprint available upon request