PHP
  Home arrow PHP arrow Page 7 - Storing PHP Sessions in a Database
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Storing PHP Sessions in a Database
By: Rich Smith
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 40
    2007-05-02


    Table of Contents:
  • Storing PHP Sessions in a Database
  • Why did they fail?
  • Overriding the session storage
  • Opening and closing the session
  • Reading and Writing Session Data
  • Cleaning up the session
  • Putting it all together
  • Finishing it up

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Storing PHP Sessions in a Database - Putting it all together
    ( Page 7 of 8 )

    On the last few pages, we've talked about a bunch of little pieces that make up the sessions class.  I thought I would take a minute and give you the complete class in one shot.

    class SessionManager {

       var $life_time;

       function SessionManager() {

          // 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 `sessions` WHERE
    `session_id` = '$newid' AND `expires` > $time";

          $rs = db_query($sql);                           
          $a = db_num_rows($rs);

          if($a > 0) {
            $row = db_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 `sessions`
    (`session_id`,`session_data`,`expires`) VALUES('$newid',
    '$newdata', $time)";

          $rs = db_query($sql);

          return TRUE;

       }

       function destroy( $id ) {

          // Build query
          $newid = mysql_real_escape_string($id);
          $sql = "DELETE FROM `sessions` WHERE `session_id` =
    '$newid'";

          db_query($sql);

          return TRUE;

       }

       function gc() {

          // Garbage Collection

                           

          // Build DELETE query.  Delete all records who have passed
    the expiration time
          $sql = 'DELETE FROM `sessions` WHERE `expires` <
    UNIX_TIMESTAMP();';

          db_query($sql);

          // Always return TRUE
          return true;

       }

    }



     
     
    >>> More PHP Articles          >>> More By Rich Smith
     

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek