SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast Takai26's Avatar
    Join Date
    Jul 2004
    Location
    Toronto
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Session handling class not properly registering sessions

    Below is the code for my session abstract, its my first shot at a session handler, just spent the last few days reading various articles and posts on it trying to figure out the problem, but no luck.

    Problem :

    It seems to generate the initial set of information, but when I tested the passing of session variables I noticed it doesn't seem to 'hold' them, or perhaps its just not updating properly. I'll do my best to describe.

    I instance the below Session class using the following code:

    PHP Code:
    <?php
    define
    ("SQL_DEBUG"TRUE);
    ob_start();
    require_once 
    "common/database.php";
    require_once 
    "common/sessions.class.php";

    //!------------- BEGIN SESSION STARTUP ----------------
    $dbSession = new Session();
    session_start();
    //!------------ END SESSION STARTUP -------------------

    if(!isset($_SESSION['counter'])) {
        
    $_SESSION['counter'] = 1;
    }

    $_SESSION['counter']++;

    echo 
    $_SESSION['counter'];
    echo 
    "<p>";
    var_dump($_SESSION);
    ob_flush();
    ?>
    When I navigate to the page, (keeping in mind I have my SQL debugs running so I can see whats being called, I get the following output

    SQL Debug: INSERT INTO sessions (sess_id, sess_time, sess_start, sess_value) VALUES ('F4223D9729B11CCE7B416FF8710DF4E1', '1107158574', '1107158574', '')

    SQL Debug: SELECT * FROM sessions WHERE sess_id = 'F4223D9729B11CCE7B416FF8710DF4E1'
    2 <-- VALUE OF $_SESSION['counter']

    array(1) { ["counter"]=> int(2) }

    SQL Debug: UPDATE sessions SET sess_time = '1107158574', sess_value = 's:12:"counter|i:2;";' WHERE sess_id = 'F4223D9729B11CCE7B416FF8710DF4E1'

    SQL Debug: DELETE FROM sessions WHERE sess_time < 1107158274

    SQL Debug: DELETE FROM sessions WHERE sess_time < 1107158274
    The rogue '2' that I've bolded is the value of $_SESSION['counter'], you'll notice that $_SESSION['counter'] is initialized with 1, then incremented to 2. So it echo's correctly, if I check my database at that type, I see the value of counter is set to 2, I see the correct session_id etc. So the initial insert of the session worked.

    If I refresh the page, or just add a ?blah=blah to the uri and enter, counter re-initializes, and the value doesn't increment, it remains at 2.

    If I then go look at my mysql sessions table, I now see multiple session entries, so it appears to be starting a NEW session every time the page loads or is refreshed.

    PHP Code:
    <?php

    class Session extends DbaseConnection  {

        var 
    $dbHandle;
        var 
    $session_table "sessions";
        var 
    $sessionId;
            
        function 
    Session() {
            
    session_set_save_handler(
                array(&
    $this'StartSession'),
                array(&
    $this'CloseSession'),
                array(&
    $this'ReadSession'),
                array(&
    $this'WriteSession'),
                array(&
    $this'DestroySession'),
                array(&
    $this'GarbageCollection')
            );
            
    $this->sessionId $sessionId;
            
    $this->dbHandle = new DbaseConnection();
             
    $this->dbHandle->Open("localhost""phuckit""mss27mlg""phuckit");
        }
        
        function 
    GenerateSessionId() {
            
    srand((double) microtime() * 1000000);
            
    $this->sessionId strtoupper(md5(uniqid(rand(), true)));
            return 
    $this->sessionId;
        }
        
        function 
    StartSession($save_path$session_name) {
            
    $sessionId $this->GenerateSessionId();
            
    session_id($sessionId);
            
    $this->dbHandle->Query("INSERT INTO $this->session_table (sess_id, sess_time, sess_start, sess_value) VALUES ('$sessionId', '" time() . "', '" time() . "', '')");
            return 
    TRUE;
        }
        
        function 
    CloseSession() {
            
    $this->GarbageCollection(0);
            return 
    TRUE;
        }
        
        function 
    ReadSession($sess_id) {
            
    $result $this->dbHandle->Query("SELECT * FROM $this->session_table WHERE sess_id = '$sess_id'");
            if(!
    $result)
                return 
    '';
            else {
                
    $result $this->dbHandle->GetRow(0);
                
    $result unserialize($result['sess_value']);
                return 
    $result;
            }
        }
        
        function 
    WriteSession($sess_id$data) {
            
    $result $this->dbHandle->Query("UPDATE $this->session_table SET sess_time = '" time() . "', sess_value = '" serialize($data) . "' WHERE sess_id = '$sess_id'");
            if(!
    $result) {
                
    $dbHandle->DebugOutput(mysql_error());
                return 
    FALSE;
            } else
                return 
    TRUE;
        }
        
        function 
    DestroySession($sess_id) {
            
    $result $this->dbHandle->Query("DELETE FROM $this->session_table WHERE sess_id = '$sess_id'");
            if(!
    $result)
                return 
    FALSE;
            else 
                return 
    TRUE;
        }
        
        function 
    GarbageCollection($life) {
            
    $session_life strtotime("-5 minutes");
            
    $result $this->dbHandle->Query("DELETE FROM $this->session_table WHERE sess_time < $session_life");
            if(!
    $result)
                return 
    FALSE;
            else 
                return 
    TRUE;
        }
    }

    ?>
    Last edited by Takai26; Jan 31, 2005 at 01:48.

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Takai26
    PHP Code:
    class Session extends DbaseConnection  {
    /* big snip */
        
    function ReadSession($sess_id) {
            
    $result $this->dbHandle->Query("SELECT * FROM $this->session_table WHERE sess_id = '$sess_id'");
            if(!
    $result)
                return 
    '';
            else {
                
    $result $this->dbHandle->GetRow(0);
                
    $result unserialize($result['sess_value']);
                return 
    $result;
            }
        }
        
        function 
    WriteSession($sess_id$data) {
            
    $result $this->dbHandle->Query("UPDATE $this->session_table SET sess_time = '" time() . "', sess_value = '" serialize($data) . "' WHERE sess_id = '$sess_id'");
            if(!
    $result) {
                
    $dbHandle->DebugOutput(mysql_error());
                return 
    FALSE;
            } else
                return 
    TRUE;
        } 
    There's no need to serialize the data before saving it to the database, or to unserialize after the retrieving from the database. PHP serializes session data before calling the write method and unserializes the return value of the read method. Try:
    PHP Code:
                /* in ReadSession */
                
    $result $this->dbHandle->GetRow(0);
                return 
    $result['sess_value']; 
    and
    PHP Code:
            /* in WriteSession */
            
    $result $this->dbHandle->Query("UPDATE $this->session_table SET sess_time = '" time() . "', sess_value = '" $data "' WHERE sess_id = '$sess_id'"); 
    You should also note that the serialization of session data is slightly different from PHP's serialize() and unserialize() functions. This is shown in your debugger output:
    s:12:"counter|i:2;";
    instead of
    s:12:"counter":i:2;
    Unless you need to manipulate session data inside your session handling methods, this won't matter.
    Zealotry is contingent upon 100 posts and addiction 200?

  3. #3
    SitePoint Enthusiast Takai26's Avatar
    Join Date
    Jul 2004
    Location
    Toronto
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've removed the serialization of the data, but am still experiencing the same problem.

    Also I've noticed that I appear to be generating a new session id everytime I navigate a page. So that would also appear to be part of the problem.
    Sean "Takai" Saulnier
    New project coming soon

  4. #4
    SitePoint Enthusiast Takai26's Avatar
    Join Date
    Jul 2004
    Location
    Toronto
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I actually found the problem, my ReadSession method wasn't returning the data properly.

    I modified my GetRow() method in my DbaseConnection class to return a mysql_fetch_array() instead of mysql_fetch_row() and that resolved all my problems.
    Sean "Takai" Saulnier
    New project coming soon


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
  •