Page 1 of 1

Problems with session_destroy()

Posted: Sun Jan 23, 2005 5:30 am
by AGISB
I am trying to get a custom session handler to work writing the session data to a mysql table. all seems to work fine except one thing I am not sure of how this is supposed to work:

I am trying to destroy the session on logout:

Code: Select all

session_start();
$_SESSION = array();
session_unset();
session_destroy();
The session data is set to 0 just fine but the database entry still remains. If I read the handler (I used one of my php book) right I see a delete query in the destroy section but it simply does not get executed. Any ideas?

here is the handler code

Code: Select all

require "database.php";
function getMicroTime()
{
  $mtime = explode(" ", microtime());
  return($mtimeї1] + $mtimeї0]);
}
$connection = NULL;
$session_table = NULL;
function sessionOpen($database_name, $table_name)
{
  global $connection;
  global $session_table;
  global $hostName;
  global $username;
  global $password;

  if (!($connection = @ mysql_connect($hostName, $username, $password)))
     showerror();

  if (!mysql_select_db($database_name, $connection))
     showerror();

  $session_table = $table_name;

  return true;
}

function sessionRead($sess_id)
{
  global $connection;

  global $session_table;

  $search_query = "SELECT * FROM {$session_table}
                   WHERE session_id = '{$sess_id}'";

  if (!($result = @ mysql_query($search_query, $connection)))
     showerror();

  if(mysql_num_rows($result) == 0)
    return "";
  else
  {
    $row = mysql_fetch_array($result);
    return $rowї"session_variable"];
  }
}

function sessionWrite($sess_id, $val)
{
  global $connection;

  global $session_table;

  $time_stamp = getMicroTime();

  $search_query = "SELECT session_id FROM {$session_table}
                   WHERE session_id = '{$sess_id}'";

  if (!($result = @ mysql_query($search_query, $connection)))
     showerror();

  if(mysql_num_rows($result) == 0)
  {
     $insert_query = "INSERT INTO {$session_table}
                      (session_id, session_variable, last_accessed)
                      VALUES ('{$sess_id}', '{$val}', {$time_stamp})";

     if (!mysql_query($insert_query, $connection))
        showerror();
  }
  else
  {
     $update_query = "UPDATE {$session_table}
                      SET session_variable = '{$val}',
                          last_accessed = {$time_stamp}
                      WHERE session_id = '{$sess_id}'";

     if (!mysql_query($update_query, $connection))
        showerror();
  }
}

function sessionClose()
{
    return true;
}

function sessionDestroy($sess_id)
{
  global $connection;

  global $session_table;

  $delete_query = "DELETE FROM {$session_table}
                   WHERE session_id = '{$sess_id}'";

  if (!($result = @ mysql_query($delete_query, $connection)))
     showerror();

  return true;
}

function sessionGC($max_lifetime)
{
  global $connection;

  global $session_table;

  $current_time = getMicroTime();

  $delete_query = "DELETE FROM {$session_table}
             WHERE last_accessed < (&#123;$current_time&#125; - &#123;$max_lifetime&#125;)";

  if (!($result = @ mysql_query($delete_query, $connection)))
     showerror();

  return true;
&#125;

session_set_save_handler("sessionOpen", 
                         "sessionClose", 
                         "sessionRead", 
                         "sessionWrite", 
                         "sessionDestroy", 
                         "sessionGC");

Posted: Sun Jan 23, 2005 7:41 am
by feyd
session_destroy() wrote:<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}

// Finally, destroy the session.
session_destroy();
?>