Database Session Class

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
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Database Session Class

Post by tecktalkcm0391 »

I am having troubles with the following Database Session Class... Everything works, except the regenerate session id feature, which is the update() function... when I have page1.php that sets a session variable, then go to page2.php and back and forth (both using and not using the browser buttons) eventually I will get an error about the session variable:

Notice: Undefined index: testing in /sandbox/sessions2.php on line 11

The code for the pages is the same except one outputs the session var, and the other makes it...
that page's code is just:

Code: Select all

 
<?php 
include(ROOT.'/private/includer.php');
$db = new DB();
$db->connect();
 
//require_once("sessions.php");
$sess = new session();
session_start();
 
echo $_SESSION['testing'];
 
session_write_close();
?> 
 
 
The session class is:

Code: Select all

<?php 
 
class session {
 
   var $life_time;
 
   function session() {
 
      // Read the maxlifetime setting from PHP
      $this->life_time = get_cfg_var("session.gc_maxlifetime");
 
      // Register this object as the session handler
      session_set_save_handler( 
        array( &$this, "open" ), 
        array( &$this, "close" ),
        array( &$this, "read" ),
        array( &$this, "write"),
        array( &$this, "destroy"),
        array( &$this, "gc" )
      );
 
   }
 
   function open( $save_path, $session_name ) {
 
      global $sess_save_path;
 
      $sess_save_path = $save_path;
 
      // Don't need to do anything. Just return TRUE.
 
      return true;
 
   }
 
   function close() {
 
      return true;
 
   }
 
   function read( $id ) {
 
      // Set empty result
      $data = '';
 
      // Fetch session data from the selected database
 
      $time = time();
 
      $newid = mysql_real_escape_string($id);
      $sql = "SELECT `session_data` FROM `php_sessions` WHERE `session_id` = '$newid' AND `last_updated` > $time";
 
      $rs = mysql_query($sql);                           
      $a = mysql_num_rows($rs);
 
      if($a > 0) {
        $row = mysql_fetch_assoc($rs);
        $data = $row['session_data'];
 
      }
 
      return $data;
 
   }
 
   function write( $id, $data ) {
 
      // Build query                
      $time = time() + $this->life_time;
 
      $newid = mysql_real_escape_string($id);
      $newdata = mysql_real_escape_string($data);
 
      $sql = "REPLACE `php_sessions` (`session_id`,`session_data`,`last_updated`) VALUES('$newid', '$newdata', $time)";
 
      $rs = mysql_query($sql);
 
      return TRUE;
 
   }
 
   function destroy( $id ) {
 
      // Build query
      $newid = mysql_real_escape_string($id);
      $sql = "DELETE FROM `php_sessions` WHERE `session_id` =
'$newid'";
 
      mysql_query($sql);
 
      return TRUE;
 
   }
 
   function gc() {
 
      // Garbage Collection
                    
 
      // Build DELETE query.  Delete all records who have passed the expiration time
      $sql = 'DELETE FROM `php_sessions` WHERE `last_updated` < UNIX_TIMESTAMP();';
 
      mysql_query($sql);
 
      // Always return TRUE
      return true;
 
   }
   
   function update() { // REGENERATES NEW SESSION ID ONLY
        $old_sess_id = session_id();
        session_regenerate_id(false);
        $new_sess_id = session_id();
           
        $query = "UPDATE `php_sessions` SET `session_id` = '$new_sess_id' WHERE session_id = '$old_sess_id'";
            mysql_query($query);
            
    }
    
    
 
}
 
 
 
?>
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Database Session Class

Post by tecktalkcm0391 »

Hello, I still can't figure this out, but you can visit this site for the sample of what is not working:

[site removed for client]/session.php --- page 1
[site removed for client]/session2.php --- page 2
Last edited by tecktalkcm0391 on Sat Jul 12, 2008 10:00 am, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Database Session Class

Post by Christopher »

Have you looked in the PHP manual? There is a working example there. Is that were your code came from? There are also a number of working libraries for this on phpclasses.org, etc.
(#10850)
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Database Session Class

Post by tecktalkcm0391 »

arborint wrote:Have you looked in the PHP manual? There is a working example there. Is that were your code came from? There are also a number of working libraries for this on phpclasses.org, etc.
Yes, I looked in the manual... Thats were I got the initial session_regen part of my code... I wrote the rest myself. It all works except keeping the user in the session when you move pages, were each one sets a new ID.

I'll look at phpclasses.org in the mean time, but any other ideas why the regen. session id isn't working in my code?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Database Session Class

Post by tecktalkcm0391 »

I'm still having trouble... even when the session id stays the same... please look at the example website in my second post... to see whats happening...

SESSIONS,php

Code: Select all

 
<?php 
include(YB_ROOT_S.'/private/master/includer.php');
$db = new DB();
$db->connect();
$db->setTable("php_sessions");
 
$sess = new session();
session_start();
 
 
$_SESSION['testing'] = 'randonmness';
 
session_write_close();
?>
<a href="sessions2.php?sid=<?php echo session_id(); ?>"> TESTING </a>
SESSIONS2.php

Code: Select all

<?php 
include(YB_ROOT_S.'/private/master/includer.php');
$db = new DB();
$db->connect();
$db->setTable("php_sessions");
 
require_once("sessions.php");
$sess = new session();
session_start();
 
echo $_SESSION['testing'];
 
 
session_write_close();
?> 
<a href="sessions.php?sid=<?php echo session_id(); ?>"> TESTING </a>
Post Reply