Hi everybody,
I am making a small site. This site uses sessions and I need to store
pictures for each session. I solved this problem thanks to the
session_set_save_handler() function. Sessions have their own folder
where all files are stored. It works quite well for the open, read and
write processes. You can find how I changed the sessions management.
But I don't know what to do at the end of a session. I know that I have
to make something with the "gc" and "destroy" functions but I don't know
what. The session ends when the browser is closed, but the files are not
deleted. What should I do ? Can someone help me to write the gc() and
destroy() functions.
Thanks in advance.
<?php
function open ($save_path, $session_name) {
global $sess_save_path, $sess_session_name;
$sess_save_path = $save_path;
$sess_session_name = $session_name;
return(true);
}
function close() {
return(true);
}
function read ($id) {
global $sess_save_path, $sess_session_name;
$sess_file = "$sess_save_path/$id/sess_$id";
if ($fp = @fopen($sess_file, "r")) {
$sess_data = fread($fp, filesize($sess_file));
return($sess_data);
} else {
return(""); // Doit retourner "" ici.
}
}
function write ($id, $sess_data) {
global $sess_save_path, $sess_session_name;
if ( ! is_dir("$sess_save_path/$id") ) {
mkdir("$sess_save_path/$id");
}
$sess_file = "$sess_save_path/$id/sess_$id";
if ($fp = @fopen($sess_file, "w")) {
return(fwrite($fp, $sess_data));
} else {
return(false);
}
}
function destroy ($id) {
global $sess_save_path, $sess_session_name;
$sess_file = "$sess_save_path/$id/sess_$id";
if ($handle = opendir($sess_save_path)) {
/* Ceci est la façon correcte de traverser un dossier. */
while (false !== ($file = readdir($handle))) {
if ( $file !== "." and $file !== ".." ) {
@unlink($file);
}
}
closedir($handle);
}
return(@unlink($sess_file));
}
/************************************************** *****
* ATTENTION - Vous devrez implémenter un *
* collecteur de données obosolètes ici. *
************************************************** *****/
function gc ($maxlifetime) {
return true;
}
session_set_save_handler ("open", "close", "read", "write", "destroy",
"gc");
?>