PHP
  Home arrow PHP arrow Page 8 - 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 - Finishing it up
    ( Page 8 of 8 )

    Throughout this article, I've talked about possible reasons for wanting your session information in a database.  I've also showed you how to create a sessions class that would replace the built-in PHP session handling.  Now I just need to show you how to implement it.

    Let's go back to our example from the beginning of the article.  You have an application built that uses session information heavily.  Most likely, you have common functions and logic broken out into include files, including the database connection and session starting code.  If you actually code the session_start() function in each file where you lose it, you'll have a bunch more work to do.

    In any case, here's the code to add to your application in order to make PHP use your class instead.  Find your session_start() code, and make it look like this:

    require_once("sessions.php");
    $sess = new SessionManager();
    session_start();

    So in the above example, we require the code.  This adds the class declaration into memory.  With the second line, we invoke the SessionManager() class, which in turn reworks the session_set_save_handler to call class functions rather than  the default ones.  And finally, the session_start() function is drawn, which begins using the new DB class right away.

    For most structured code, the hardest part of the scenario would be to replace the db_() calls with calls from your own application's db api.  Once your class is created it takes mere seconds to implement it to your code.  Copy that code across all of your web servers, and they instantly begin using the database instead of files to read/write as default.

    One last warning before I go.  Depending on how your code is structured, PHP does not always automatically save any session data.  To be certain you are retaining your session data all the time, be sure to call the session_write_close() function at the end of each page.

    I hope the information I've shared with you during this article has, at a minimum, helped you understand how and why you would approach moving session data from files to a database.  Whether you use the example code I've supplied, or create your own from scratch, its been my goal to educate you on the whys and hows, so you can make the best decisions for your own applications.



     
     
    >>> 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