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_sav e_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_n ame;
$sess_save_path = $save_path;
$sess_session_n ame = $session_name;
return(true);
}
function close() {
return(true);
}
function read ($id) {
global $sess_save_path , $sess_session_n ame;
$sess_file = "$sess_save_pat h/$id/sess_$id";
if ($fp = @fopen($sess_fi le, "r")) {
$sess_data = fread($fp, filesize($sess_ file));
return($sess_da ta);
} else {
return(""); // Doit retourner "" ici.
}
}
function write ($id, $sess_data) {
global $sess_save_path , $sess_session_n ame;
if ( ! is_dir("$sess_s ave_path/$id") ) {
mkdir("$sess_sa ve_path/$id");
}
$sess_file = "$sess_save_pat h/$id/sess_$id";
if ($fp = @fopen($sess_fi le, "w")) {
return(fwrite($ fp, $sess_data));
} else {
return(false);
}
}
function destroy ($id) {
global $sess_save_path , $sess_session_n ame;
$sess_file = "$sess_save_pat h/$id/sess_$id";
if ($handle = opendir($sess_s ave_path)) {
/* Ceci est la façon correcte de traverser un dossier. */
while (false !== ($file = readdir($handle ))) {
if ( $file !== "." and $file !== ".." ) {
@unlink($file);
}
}
closedir($handl e);
}
return(@unlink( $sess_file));
}
/*************** *************** *************** **********
* ATTENTION - Vous devrez implémenter un *
* collecteur de données obosolètes ici. *
*************** *************** *************** **********/
function gc ($maxlifetime) {
return true;
}
session_set_sav e_handler ("open", "close", "read", "write", "destroy",
"gc");
?>
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_sav e_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_n ame;
$sess_save_path = $save_path;
$sess_session_n ame = $session_name;
return(true);
}
function close() {
return(true);
}
function read ($id) {
global $sess_save_path , $sess_session_n ame;
$sess_file = "$sess_save_pat h/$id/sess_$id";
if ($fp = @fopen($sess_fi le, "r")) {
$sess_data = fread($fp, filesize($sess_ file));
return($sess_da ta);
} else {
return(""); // Doit retourner "" ici.
}
}
function write ($id, $sess_data) {
global $sess_save_path , $sess_session_n ame;
if ( ! is_dir("$sess_s ave_path/$id") ) {
mkdir("$sess_sa ve_path/$id");
}
$sess_file = "$sess_save_pat h/$id/sess_$id";
if ($fp = @fopen($sess_fi le, "w")) {
return(fwrite($ fp, $sess_data));
} else {
return(false);
}
}
function destroy ($id) {
global $sess_save_path , $sess_session_n ame;
$sess_file = "$sess_save_pat h/$id/sess_$id";
if ($handle = opendir($sess_s ave_path)) {
/* Ceci est la façon correcte de traverser un dossier. */
while (false !== ($file = readdir($handle ))) {
if ( $file !== "." and $file !== ".." ) {
@unlink($file);
}
}
closedir($handl e);
}
return(@unlink( $sess_file));
}
/*************** *************** *************** **********
* ATTENTION - Vous devrez implémenter un *
* collecteur de données obosolètes ici. *
*************** *************** *************** **********/
function gc ($maxlifetime) {
return true;
}
session_set_sav e_handler ("open", "close", "read", "write", "destroy",
"gc");
?>