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

Why did they fail? - 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



Sessions, by default, write to a temporary file on the web server.  So if you choose to store data in your session (a user's name, for example), it is available on any page just by reading from the session.  This works great, until you bring more servers into the equation.

Think about this for a moment.  Let's say you have three web servers, all with the same website on them.  Furthermore, these web servers are set up as a round robin.  A round robin means that when a new request comes in, it's handed to the next server in the series.  So with three web servers, requests would be handled in the order of "1, 2, 3, 1, 2, 3, etc."  This means that, as a visitor is surfing your site, they are potentially visiting different servers in the same session.  With an intelligent load balancer, the handling of connections is not as crude, but it is still possible for a user to visit a different server with each click of the mouse.

So now, let's take the users on your site.  As they click through your web pages, they will be moving from server to server.  So if you saved something to a session variable while you were on server 1, it would not be available to you if your next click took you to server 3.  This doesn't mean that you coded your application incorrectly, it merely means you need to reconsider the session configuration.

With more than one server hosting the same website, your options narrow.  If you wish to keep using disk space to store your session information, then all of your web servers need to mount the same share, so they all have access to the file.  Another option, and the one we are going to explore in better detail, is to store your session inside a database instead of on disk.  This way, your session information is available no matter which web server you are on.

Luckily, PHP has a built-in ability to override its default session handling.  The function session_set_save_handler() lets the programmer specify which functions should actually be called when it is time to read or write session information.

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