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

Finishing it up - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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