PHP
  Home arrow PHP arrow Page 3 - 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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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? 
PHP

Storing PHP Sessions in a Database
By: Rich Smith
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 30
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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 - Overriding the session storage


    (Page 3 of 8 )

    As I just said, the session_set_save_handler() function will allow us to override the default method of storing data.  According to the documentation, here is the format for this function:

    bool session_set_save_handler ( callback $open, callback $close, callback $read, callback $write, callback $destroy, callback $gc );

    In our example, I'm going to create a session class that can be used to store information to the database instead of to a file.

    For starters, create a new file called "sessions.php."  Inside this file, put the following code:

    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" )
          );

       }

    }

    In the above example, the SessionManager() class and its constructor are created.  You will notice that instead of merely passing function names into the session_set_save_handler() function, I sent arrays allowing me to identify class methods as the intercepts for the session actions.

    More PHP Articles
    More By Rich Smith


       · I actually needed this logic for a recent implementation. I thought my experience...
       · Do people still consider using 'global' to be a good idea? Wouldn't it be better to...
       · Actually, you are correct. My own session class has alot of extra site-specific...
       · Hey, Rich, I'm glad you took the time to put this into an article. It's a really...
       · I require this session after login and encounter following error.Fatal error:...
       · Yes. You need to change the DB logic in the class to use your DB calls, or straight...
       · Sorry but I still don't understand. I created the exact same table "session" like...
       · The db_query() call is a custom db class that my application uses. If you are using...
     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway