PHP
  Home arrow PHP arrow Page 4 - 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? 
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 - Opening and closing the session
    ( Page 4 of 8 )

    On the previous page, when we called session_set_save_handler() to override the session handling functions, the first two arguments passed were for the open and close logic.  Let's add these methods to our new class, then take a closer look.

         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;

         }

    The above code should be added inside the SessionManager class we started on the previous page.

    Taking a close look, you will see that, for the most part, we didn't do anything in the open and close methods.  That would be because we will be writing our information to a database.  In this lesson, I am assuming that the application you are adding this to ALREADY has an open database connection to your database.  If this is the case, then the above code is good as it is.  If not, then the open function would need to include code for creating your database connection, and the close would close said connection.

    If we were writing a new file-handling session manager, the open function would handle opening the file descriptor.  The close function would then close said file descriptor, to prevent data from being lost.

    So far, so good.  But before we get to the point where we are actually writing the session information into the database, we need some place to put it. In your application database, you will need to create a "sessions" table to store the session information.  Here's the one I defined, in MySQL create format:

    CREATE TABLE `sessions` (

      `session_id` varchar(100) NOT NULL default '',

      `session_data` text NOT NULL,

      `expires` int(11) NOT NULL default '0',

      PRIMARY KEY  (`session_id`)

    ) TYPE=MyISAM;

    I would definitely suggest adding this table to your application's database.  Not only does it keep it together with everything else, but it also makes it possible to share the same database connections for your sessions and your application itself.



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

       

    PHP ARTICLES

    - 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 ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT