Ciao
Devo salvare le sessioni su di un DB e non utilizzarle in maniera standard su file.
Ho trovato questo script che ho modificato a mio piacere:
(sessionX.php)
Codice PHP:
<?
mysql_connect("localhost","usr","pass");
mysql_select_db("sessioni");
function on_session_start($save_path, $session_name) {
error_log($session_name . " ". session_id());
}
function on_session_end() {
// Nothing needs to be done in this function
// since we used persistent connection.
}
function on_session_read($key) {
error_log($key);
$stmt = "select session_data from sessions ";
$stmt .= "where session_id ='$key' ";
$stmt .= "and unix_timestamp(session_expiration) > unix_timestamp(now())";
$sth = mysql_query($stmt);
if($sth)
{
$row = mysql_fetch_array($sth);
return($row['session_data']);
}
else
{
return $sth;
}
}
function on_session_write($key, $val) {
error_log("$key = $value");
$val = addslashes($val);
$insert_stmt = "insert into sessions values('$key', ";
$insert_stmt .= "'$val',date_add(now(), interval 10 SECOND))";
$update_stmt = "update sessions set session_data ='$val', ";
$update_stmt .= "session_expiration =date_add(now(), interval 10 SECOND))";
$update_stmt .= "where session_id ='$key '";
// First we try to insert, if that doesn't succeed, it means
// session is already in the table and we try to update
mysql_query($insert_stmt);
$err = mysql_error();
if ($err != 0)
{
error_log( mysql_error());
mysql_query($update_stmt);
}
}
function on_session_destroy($key) {
mysql_query("delete from sessions where session_id = '$key'");
}
function on_session_gc($max_lifetime)
{
mysql_query("delete from sessions where unix_timestamp(session_expiration) < unix_timestamp(now())");
}
// Set the save handlers
session_set_save_handler("on_session_start", "on_session_end",
"on_session_read", "on_session_write",
"on_session_destroy", "on_session_gc");
session_start();
?>
Ho messo una durata sessione di soli 10 secondi per vedere se tutto funzionava.
Ho creato un file:
Codice PHP:
<?php
include "sessionX.php";
$_SESSION['varA']=5;
$_SESSION['varB']=10;
echo date('H:i:s');
?>
E nel DB il record viene scritto... aspetto 30 secondi (per sicurezza) e lancio una chiamata ad un file identico al precedente, variando ovviamente le varA e varB.
Dovrei avere quanto meno una modifica dei valori sul DB (anzi... una cancellazione della vecchia sessione scaduta e una ricreazione della sessione coi nuovi valori)... ma non succede una mazza!
Dove sbaglio???
Grazie