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

    There are many reasons to utilize sessions when creating a web-based application using PHP. Session information, by default, is stored in a file on your web server. But what if that becomes a problem? In this article, I'll talk about why you might want to move your PHP sessions to a database, and show you how to do it.

    So you've finished your largest project yet, a robust order-taking system for a very successful company.  To give the best user experience possible, you made heavy use of sessions in order to keep information about the customer handy, from page to page, as any good PHP programmer would.  You deliver the site to your customer and it goes live, to great fanfare.  Traffic is slow at first, but as the customers begin to use it, traffic picks up and in a few months the site is serving over 5 million hits a day, between shoppers, search engines, and actual customers.  The web server is dying under the stress, and the company turns to you to figure out how to increase their bandwidth.

    A quick analysis of the database shows you that its usage is quite low.  It's the actual load on the web server that's causing the issue.  Your code is tight and uses caching where available (as is obvious by the low load on the database).  The problem seems to be the sheer traffic on your web server.  How do you cope?

    The above problem is one we should all be so lucky to face: being overly popular.  If your website is as wildly successful as the one in the above example, it means you should have the resources available to address this kind of issue.

    And just how do you fix this problem?  In our example, let's address the problem by adding web server(s).  It doesn't matter if you round-robin them, or add them to a true load balancer.  But adding additional servers will allow the load to be split among more than one machine, allowing each of them to serve data in a more efficient fashion.

    This will solve the immediate load problem.  But what will it do to your application?

    For those of you who use sessions but have never written an application for multi-server distribution, you may be surprised to know that your sessions will fail miserably if left alone.  Why is that?



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

       

    PHP ARTICLES

    - 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 ...
    - Method Chaining in PHP 5





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