Execute Script on Session End

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jeffrey.pry
Forum Newbie
Posts: 4
Joined: Sat May 16, 2009 7:10 pm

Execute Script on Session End

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Execute Script on Session End

Post by Benjamin »

See: http://us3.php.net/manual/en/function.s ... andler.php

You'll want to define a custom gc routine.
User avatar
jeffrey.pry
Forum Newbie
Posts: 4
Joined: Sat May 16, 2009 7:10 pm

Re: Execute Script on Session End

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Execute Script on Session End

Post by Benjamin »

If I answered that I would be referencing the link I already provided.
User avatar
jeffrey.pry
Forum Newbie
Posts: 4
Joined: Sat May 16, 2009 7:10 pm

Re: Execute Script on Session End

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Execute Script on Session End

Post 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.
User avatar
jeffrey.pry
Forum Newbie
Posts: 4
Joined: Sat May 16, 2009 7:10 pm

Re: Execute Script on Session End

Post 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
Post Reply