Page 1 of 1

Execute Script on Session End

Posted: Sat May 16, 2009 7:14 pm
by jeffrey.pry
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

Re: Execute Script on Session End

Posted: Sat May 16, 2009 7:17 pm
by Benjamin
See: http://us3.php.net/manual/en/function.s ... andler.php

You'll want to define a custom gc routine.

Re: Execute Script on Session End

Posted: Sat May 16, 2009 7:19 pm
by jeffrey.pry
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

Re: Execute Script on Session End

Posted: Sun May 17, 2009 1:49 am
by Benjamin
If I answered that I would be referencing the link I already provided.

Re: Execute Script on Session End

Posted: Sun May 17, 2009 7:18 pm
by jeffrey.pry
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

Re: Execute Script on Session End

Posted: Mon May 18, 2009 10:00 am
by Benjamin
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
 
?>
 
 
GC stands for garbage collection. You'll want to use the code above and modify the GC function to delete your records. Cron is also a good solution. I see no reason not to use it.

Re: Execute Script on Session End

Posted: Mon May 18, 2009 11:28 am
by jeffrey.pry
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