SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Session Garbage Collection

    I wrote the following class to store sessions in my database:
    PHP Code:
    <?php

        
    /**
        * Project:        Kayzio Framework
        * File:        Session/main.class.php
        *
        * @copyright 2007 Kayzio. All Rights Reserved.
        * @author Matthew Dahl <dahl.matthew@gmail.com>
        * @package Kayzio Framework
        */

        /***********************************************************************************/

        
    class SessionManager {

            
    /*
            * Database Type
            *
            * @var string
            */
            
    var $dbType "mysql";

            
    /*
            * Session Life Time
            *
            * @var int
            */
            
    var $LifeTime;

            
    /***********************************************************************************/

            /*
            * Session Manager
            */
            
    function SessionManager() {
                
    $this->LifeTime get_cfg_var("session.gc_maxlifetime");
                
    session_set_save_handler
                array(&
    $this"Open"), 
                array(&
    $this"Close"),
                array(&
    $this"Read"),
                array(&
    $this"Write"),
                array(&
    $this"Destroy"),
                array(&
    $this"GC")
                );
            }

            
    /*
            * Open Session
            *
            * @params string, string
            */
            
    function Open($SavePath$SessionName) {
                global 
    $sess_save_path;
                
    $sess_save_path $SavePath;
                return 
    true;
            }

            
    /*
            * Close Session
            */
            
    function Close() {
                return 
    true;
            }

            
    /*
            * Read Session
            *
            * @param int
            */
            
    function Read($ID) {
                
    $Data "";
                
    $Time time();
                if (
    strtolower($this->dbType) == "mssql") {
                    
    $NewID addslashes($ID);
                    
    $ReadSession mssql_query("SELECT data FROM sessions WHERE id = '$NewID' AND expires > $Time");                           
                    
    $NumRows mssql_num_rows($ReadSession);
                    if(
    $NumRows 0) {
                        
    $Row mssql_fetch_assoc($ReadSession);
                        
    $Data $Row['data'];
                    }
                    return 
    $Data;
                }
                elseif (
    strtolower($this->dbType) == "mysqli") {
                    
    $NewID mysqli_real_escape_string($ID);
                    
    $ReadSession mysqli_query("SELECT data FROM sessions WHERE id = '$NewID' AND expires > $Time");                           
                    
    $NumRows mysqli_num_rows($ReadSession);
                    if(
    $NumRows 0) {
                        
    $Row mysqli_fetch_assoc($ReadSession);
                        
    $Data $Row['data'];
                    }
                    return 
    $Data;
                }
                elseif (
    strtolower($this->dbType) == "pgsql") {
                    
    $NewID pg_escape_string($ID);
                    
    $ReadSession pg_query("SELECT data FROM sessions WHERE id = '$NewID' AND expires > $Time");                           
                    
    $NumRows pg_num_rows($ReadSession);
                    if(
    $NumRows 0) {
                        
    $Row pg_fetch_assoc($ReadSession);
                        
    $Data $Row['data'];
                    }
                    return 
    $Data;
                }
                else {
                    
    $NewID mysql_real_escape_string($ID);
                    
    $ReadSession mysql_query("SELECT data FROM sessions WHERE id = '$NewID' AND expires > $Time");                           
                    
    $NumRows mysql_num_rows($ReadSession);
                    if(
    $NumRows 0) {
                        
    $Row mysql_fetch_assoc($ReadSession);
                        
    $Data $Row['data'];
                    }
                    return 
    $Data;
                }
            }

            
    /*
            * Write Session
            *
            * @params int, string
            */
            
    function Write($ID$Data) {          
                
    $Time time() + $this->LifeTime;
                if (
    strtolower($this->dbType) == "mssql") {
                    
    $NewID addslashes($ID);
                    
    $NewData addslashes($Data);
                    
    $DeleteOld mssql_query("DELETE FROM sessions WHERE id = '$NewID'");
                    
    $WriteSession mssql_query("INSERT INTO sessions (id, data, expires) VALUES ('$NewID', '$NewData', $Time)");
                }
                elseif (
    strtolower($this->dbType) == "mysqli") {
                    
    $NewID mysqli_real_escape_string($ID);
                    
    $NewData mysqli_real_escape_string($Data);
                    
    $DeleteOld mysqli_query("DELETE FROM sessions WHERE id = '$NewID'");
                    
    $WriteSession mysqli_query("INSERT INTO sessions (id, data, expires) VALUES ('$NewID', '$NewData', $Time)");
                }
                elseif (
    strtolower($this->dbType) == "pgsql") {
                    
    $NewID pg_escape_string($ID);
                    
    $NewData pg_escape_string($Data);
                    
    $DeleteOld pg_query("DELETE FROM sessions WHERE id = '$NewID'");
                    
    $WriteSession pg_query("INSERT INTO sessions (id, data, expires) VALUES ('$NewID', '$NewData', $Time)");
                }
                else {
                    
    $NewID mysql_real_escape_string($ID);
                    
    $NewData mysql_real_escape_string($Data);
                    
    $DeleteOld mysql_query("DELETE FROM sessions WHERE id = '$NewID'");
                    
    $WriteSession mysql_query("INSERT INTO sessions (id, data, expires) VALUES ('$NewID', '$NewData', $Time)");
                }
                return 
    true;
            }

            
    /*
            * Destroy Session
            *
            * @params int
            */
            
    function Destroy($ID) {
                if (
    strtolower($this->dbType) == "mssql") {
                    
    $NewID addslashes($ID);
                    
    mssql_query("DELETE FROM sessions WHERE id = '$NewID'");
                }
                elseif (
    strtolower($this->dbType) == "mysqli") {
                    
    $NewID mysqli_real_escape_string($ID);
                    
    mysqli_query("DELETE FROM sessions WHERE id = '$NewID'");
                }
                elseif (
    strtolower($this->dbType) == "pgsql") {
                    
    $NewID pg_escape_string($ID);
                    
    pg_query("DELETE FROM sessions WHERE id = '$NewID'");
                }
                else {
                    
    $NewID mysql_real_escape_string($ID);
                    
    mysql_query("DELETE FROM sessions WHERE id = '$NewID'");
                }
                return 
    true;
            }

            
    /*
            * Garbage Collection
            */
            
    function GC() {
                
    $Time time();
                if (
    strtolower($this->dbType) == "mssql") {
                    
    mssql_query("DELETE FROM sessions WHERE expires < $Time");
                }
                elseif (
    strtolower($this->dbType) == "mysqli") {
                    
    mysqli_query("DELETE FROM sessions WHERE expires < $Time");
                }
                elseif (
    strtolower($this->dbType) == "pgsql") {
                    
    pg_query("DELETE FROM sessions WHERE expires < $Time");
                }
                else {
                    
    mysql_query("DELETE FROM sessions WHERE expires < $Time");
                }
                return 
    true;
            }

        }

        
    /***********************************************************************************/

    ?>
    The class writes sessions and reads them, but is not performing the garbage collection which is supposed to remove expired sessions. I thought it did this automatically but it is not working, does anyone know what my problem is? Thanks in advance,
    Matt
    Kayzio - We don't hesitate, we accelerate.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My guess is that PHP is destroying objects (including your SessionManager) before performing session garbage collection. As a quick test make just the GC a simple function and see if it gets called.

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you are right, I tried adding header("Location: ../index.php"); and it didn't redirect or give an error. Do you know how to fix this problem?
    Kayzio - We don't hesitate, we accelerate.

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found one way to run the garbage collection, I added $this->GC(); at the beginning of the Write function so whenever it goes to write a session it deletes expired sessions first. Does this sound like a bad idea to do or not.
    Kayzio - We don't hesitate, we accelerate.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It sounds bad, but might not be bad. It feels a bit hackish. I don't think it's dangerous, though.

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea that is kinda how I feel about it.
    Kayzio - We don't hesitate, we accelerate.

  7. #7
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're not using PHP5, are you?

  8. #8
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, PHP 5.1.4. I'm running the latest version of XAMPP with the Tomcat plugin for Java. This is on my local PC for development testing.
    Kayzio - We don't hesitate, we accelerate.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •