I have fighting with the session_set_save_handler function (I was died)
I found an bug (or not bug), my problem it was that never save the session file in my folder sesion location.
You must open and read the original session file, and now you can open other session file and write in this.
This is my code:
PHP Code:
<?php
class Sesion {
var $tmp;
var $real;
var $fp = false;
var $fpr = false;
var $tempo = 0;
function Sesion () {
$this->tempo = 60*60;
ini_set('session.use_cookies', '1');
ini_set('session.gc_maxlifetime', $this->tempo);
ini_set('session.save_handler', 'user');
}
function inicia () {
session_set_save_handler(
array(&$this, "abrir"),
array(&$this, "pechar"),
array(&$this, "ler"),
array(&$this, "escribir"),
array(&$this, "destruir"),
array(&$this, "caducar")
);
}
function abrir ($save_path, $session_name) {
global $paths;
$this->tmp = $save_path."/sess_".session_id();
$this->real = $paths["session"].base64_encode(session_id());
return (true);
}
function pechar () {
flock($this->fpr, LOCK_UN);
fclose($this->fpr);
flock($this->fp, LOCK_UN);
fclose($this->fp);
return (true);
}
function ler ($id) {
if ($this->fpr = @fopen($this->real, "r")) {
flock($this->fpr, LOCK_EX);
$datos = @fread($this->fpr, @filesize($this->real));
}
if ($this->fp = @fopen($this->tmp, "r")) {
flock($this->fp, LOCK_EX);
$sess_data = @fread($this->fp, @filesize($this->tmp));
return $datos;
} else {
return (""); // Must return "" here.
}
}
function escribir ($id, $sess_data) {
if ($this->fpr = @fopen($this->real, "w")) {
flock($this->fpr, LOCK_EX);
@fwrite($this->fpr, $sess_data);
}
if ($this->fp = @fopen($this->tmp, "w")) {
flock($this->fp, LOCK_EX);
return (@fwrite($this->fp, "1"));
} else {
return (false);
}
}
function destruir ($id) {
unlink($this->real);
return unlink($this->tmp);
}
function caducar ($maxlifetime) {
$dir = dirname($this->real);
$dp = opendir($dir);
while ($cada = readdir($dp)) {
if ($cada != "."
&& $cada != ".."
&& time()-fileatime($dir."/".$cada) > $maxlifetime) {
@unlink($dir."/".$cada);
}
}
closedir($dp);
$dir = dirname($this->tmp);
$dp = opendir($dir);
while ($cada = readdir($dp)) {
echo time()."-".fileatime($dir."/".$cada)." > $maxlifetime == ".(time()-fileatime($dir."/".$cada))."\n<br>\n";
if ($cada != "."
&& $cada != ".."
&& ereg("^sess_", $cada)
&& time()-fileatime($dir."/".$cada) > $maxlifetime) {
echo time()."-".fileatime($dir."/".$cada)." > $maxlifetime";
@unlink($dir.$cada);
}
}
closedir($dp);
return true;
}
}
$sesion = &new Sesion();
$sesion->inicia();
?>
And now It's work. I save a false data in the PHP session and a real data in my file session.
if I not open the original file session or change the path or name, it not work.
This code it's a piece of a real and more extended code.
Bookmarks