Home arrow PHP arrow Page 4 - Storing PHP Sessions in a Database

Opening and closing the session - PHP

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.

  1. Storing PHP Sessions in a Database
  2. Why did they fail?
  3. Overriding the session storage
  4. Opening and closing the session
  5. Reading and Writing Session Data
  6. Cleaning up the session
  7. Putting it all together
  8. Finishing it up
By: Rich Smith
Rating: starstarstarstarstar / 54
May 02, 2007

print this article



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


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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: