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  
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 
Actuate Whitepapers 
VeriSign Whitepapers 
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 / 28
    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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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


       · 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

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





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