i just tried the session_set_save_handler script from Sterling Hughes "PHP Cookbook", which allows to save the session in a MySQL database.
However, i ran into a problem: The script works just fine and also saves the session in the database, but it doesn't update the value of the saved data. I browsed google for additional examples, but only to find out, that the exact same problem appeared. So, my guess is, that my script has some kind of logical error, but i am unable to spot it. Any help is more than welcome (regarding my starting headache ;) )
Here is the script:
If the session_set_save_handler function is commented (as seen below), then the script starts the session and continues to display the increasing $counter variable with every page reload. However, once you uncomment the session_set_save_handler function, the session gets saved in the MySQL database, but the counter variable won't increase and always stays at "1". (as i said, i tried this with several scripts already, but all resulted in the same problem)
So, i think the problem is either in the on_session_write() part, where the value doesn't increase, or -as already pointed out- i am having a logical error with the $counter variable. ...just to clarify: $counter++; also means that it should be increased in the MySQL database, right? Or would i have to do s.th. else to update the session values in the MySQL database?
The script:
<? function on_session_start($save_path, $session_name) { // nothing }
function on_session_end() { // nothing }
function on_session_read($key) { $db = mysql_connect(DB_HOST,DB_USER,DB_PASS); mysql_select_db(DB_DATABASE); if ($db) { $query = mysql_query('SELECT session_data FROM sessions WHERE session_id="'.$key.'" AND session_expiration > now()'); $row = mysql_fetch_row($query); } return $row[0]; mysql_close(); }
function on_session_write($key, $val) { $val = addslashes($val); $db = mysql_connect(DB_HOST,DB_USER,DB_PASS); mysql_select_db(DB_DATABASE); if ($db) { $query = mysql_query('INSERT INTO sessions VALUES("'.$key.'", "'.$val.'", now() + 3600)'); if (!$query) $queryb = mysql_query('UPDATE sessions SET session_data="'.$val.'", session_expiration=now()+3600 WHERE session_id="'.$key.'"'); if (!$queryb) die(sprintf("$val - $key")); } mysql_close(); }
function on_session_destroy($key) { $db = mysql_connect(DB_HOST,DB_USER,DB_PASS); mysql_select_db(DB_DATABASE); if ($db) { mysql_query('DELETE FROM sessions WHERE session_id="'.$key.'"'); } mysql_close(); }
function on_session_gc($max_lifetime) { $db = mysql_connect(DB_HOST,DB_USER,DB_PASS); mysql_select_db(DB_DATABASE); if ($db) { mysql_query('DELETE FROM sessions WHERE session_expiration < now()'); } mysql_close(); }
/* session_set_save_handler('on_session_start', 'on_session_end', 'on_session_read', 'on_session_write', 'on_session_destroy', 'on_session_gc'); */
session_start();
session_register('counter');
$counter++;
print $counter;
//session_destroy(); ?>
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php