I have a shopping cart application where users add items and their input is saved into a MySQL database and referenced by their PHP session code. One issue is that the contents of their cart remain in the database table indefinitely unless manually removed. Are there any functions within PHP that will allow me to execute a DELETE statement upon session end. I don't want to go the route of CRON jobs. I used to be able to do this in ASP with the global.asa file; there was a section that allowed you to run ASP VBScript code on session end.
Thanks for your help,
Jeffrey Kevin Pry
Pry Web Services
Execute Script on Session End
Moderator: General Moderators
- jeffrey.pry
- Forum Newbie
- Posts: 4
- Joined: Sat May 16, 2009 7:10 pm
Re: Execute Script on Session End
See: http://us3.php.net/manual/en/function.s ... andler.php
You'll want to define a custom gc routine.
You'll want to define a custom gc routine.
- jeffrey.pry
- Forum Newbie
- Posts: 4
- Joined: Sat May 16, 2009 7:10 pm
Re: Execute Script on Session End
Am I correct in assuming the GC runs on session end? (Via destroy or timeout)? Can you provide a quick example?
Thanks again,
Jeffrey Kevin Pry
Pry Web Services
Thanks again,
Jeffrey Kevin Pry
Pry Web Services
Re: Execute Script on Session End
If I answered that I would be referencing the link I already provided.
- jeffrey.pry
- Forum Newbie
- Posts: 4
- Joined: Sat May 16, 2009 7:10 pm
Re: Execute Script on Session End
I looked at the resource you provided and as I am new to PHP I have some trouble understanding where to put it and how to write the code. I assume I will put the GC routine on the entrance page above session_start. I just don't understand how it works. I have just always had trouble understanding the PHP manuals. Your help is much appreciated.
Thanks again,
Jeffrey Kevin Pry
Pry Web Services
Thanks again,
Jeffrey Kevin Pry
Pry Web Services
Re: Execute Script on Session End
The following example provides file based session storage similar to the PHP sessions default save handler files . This example could easily be extended to cover database storage using your favorite PHP supported database engine.
Code: Select all
<?php
function open($save_path, $session_name)
{
global $sess_save_path;
$sess_save_path = $save_path;
return(true);
}
function close()
{
return(true);
}
function read($id)
{
global $sess_save_path;
$sess_file = "$sess_save_path/sess_$id";
return (string) @file_get_contents($sess_file);
}
function write($id, $sess_data)
{
global $sess_save_path;
$sess_file = "$sess_save_path/sess_$id";
if ($fp = @fopen($sess_file, "w")) {
$return = fwrite($fp, $sess_data);
fclose($fp);
return $return;
} else {
return(false);
}
}
function destroy($id)
{
global $sess_save_path;
$sess_file = "$sess_save_path/sess_$id";
return(@unlink($sess_file));
}
function gc($maxlifetime)
{
global $sess_save_path;
foreach (glob("$sess_save_path/sess_*") as $filename) {
if (filemtime($filename) + $maxlifetime < time()) {
@unlink($filename);
}
}
return true;
}
session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
session_start();
// proceed to use sessions normally
?>
- jeffrey.pry
- Forum Newbie
- Posts: 4
- Joined: Sat May 16, 2009 7:10 pm
Re: Execute Script on Session End
Thanks! That's exactly what I was hoping for. I agree with you that CRON is a good solution, but I am running IIS 7 w/ PHP via FastCGI. That's my first issue, I know. Not much of an IIS fan, but it's what I have to use.
Thanks again,
Jeffrey Kevin Pry
Pry Web Services
Thanks again,
Jeffrey Kevin Pry
Pry Web Services