sto impazzendo a costruire un gestore di sessioni personalizzato, ma se con php5 funziona benissimo, con php4 va a metà e non capisco cosa sia sbagliato...
questa è la versione MOLTO tartassata della classe che stavo facendo.
VA DETTO però, che la uso "dentro" un'altra classe figlia, da cui chiamo il metodo start...
è forse una diversità tra i 2 php??? in php4 il metodo write non funziona, quindi non scrive le sessioni!...
sapete aiutarmi?
Codice PHP:
<?php
class session
{
# var $id; #id di sessione #private (non serve, c'è già session_id)
# var $info = ''; #dati di sessione,togli! #private
var $path = ''; #dir dei dati #protected
var $name = 'userid'; #nome sessione ALFANUMERICO!(al posto di PHPSESSID) #private const (es. self::name)
var $life = '1 day'; #massima inattività (gc) #private const (es. self::life)
var $srcfile; #handle per fopen o db sqlite (usare id???)
// prototipo di file sessione: 569s7dd4567gd3f5g6ga725.userid
function session() # __construct
{
# ini_set('session.use_cookies', '1'); // ???
# ini_set('session.save_handler', 'files');
# ini_set('session.gc_maxlifetime', strtotime($this->life, 0));
# session_name($this->name); # parametro usato dalle funzioni
# session_save_path(''); # parametro usato dalle funzioni
}
function start() #protected
{
# ini_set('session.use_cookies', '1'); // ???
# ini_set('session.save_handler', 'files');
ini_set('session.gc_maxlifetime', strtotime($this->life, 0));
session_name($this->name); # parametro usato dalle funzioni
session_save_path($this->path); # parametro usato dalle funzioni
session_set_save_handler
( array(&$this, 'open')
, array(&$this, 'close')
, array(&$this, 'read')
, array(&$this, 'write')
, array(&$this, 'destroy')
, array(&$this, 'gc')
);
register_shutdown_function('session_write_close');
session_start();
}
#----- "Safe mode" for (un)registering session variables ------#
function set($s_var, $value = null) #public final
{
if ($value !== null)
$_SESSION[$s_var] = $value;
else unset($_SESSION[$s_var]);
return true;
}
#----- "Safe mode" for reading session variables ------#
function get($s_var = '') #public final
{
if (!$s_var) return empty($_SESSION);
return array_key_exists($s_var, $_SESSION) ? $_SESSION[$s_var] : null;
}
function open($s_path, $s_name) #private final
{
# $this->id = session_id();
$this->srcfile = sprintf('%s/%s.%s', $s_path, session_id(), $s_name);
echo 'FILE = '.$this->srcfile;
# CREARE FILE SE NON ESISTE!
// trova il più veloce: file_exists, file, is_file.
if(!file_exists($this->srcfile))
{
$f = fopen($this->srcfile, 'w');
fclose($f);
}
# @return: TRUE | FALSE
return true;
}
function close() #private final
{
return true;
# @return: TRUE | FALSE
# chiama gc()
}
function read($s_id) #private final
{
$s_data = file_get_contents($this->srcfile);
echo 'dati letti = '.$s_data;
return '';#($s_data) ? $s_data : '';
# @return: datastring oppure ''
}
function write($s_id, $s_data) #private final
{
# echo 'dati scritti = '.$s_data;
$handle = fopen($this->srcfile, 'wb');
fseek($handle, 0);
fwrite($handle, $s_data);
fclose($handle);
# return file_put_contents($this->srcfile, $s_data) ? true : false;
//scrive $data
# @return: TRUE | FALSE
}
function destroy($s_id) #private final
{
return unlink($this->srcfile);
# @return: TRUE | FALSE
}
function gc($s_maxlifetime) #private final
{/*
foreach (glob($this->path.'*.'.$this->name) as $file)
if (time()-fileatime($file) > $s_maxlifetime)
if (!unlink($file)) { break; return false; }
clearstatcache();
*/
return true;
# @return: TRUE | FALSE
}
}
#echo 'ciao'.time().'
';
$sess = &new session;
$sess->start();
#session_destroy();
if(!empty($_GET['sess'])) $sess->set($_GET['sess'], $_GET['sessv']);
#echo $sess->set('false', 'ciao');
print_r($_SESSION);
?>