Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Saving PHP Session Data to a Database

By Tony Marston
2005-07-11


Define database table

This is how I have defined the database table which will hold all my session data:

CREATE TABLE `php_session` (

`session_id` varchar(32) NOT NULL default '',
`user_id` varchar(16) default NULL,
`date_created` datetime NOT NULL default '0000-00-00 00:00:00',
`last_updated` datetime NOT NULL default '0000-00-00 00:00:00',
`session_data` longtext,
PRIMARY KEY (`session_id`),
KEY `last_updated` (`last_updated`)
) ENGINE=MyISAM

Please note the following:

session_idThis is the identity of the session, so it must be the primary key.
session_dataThis must be big enough to hold the largest $_SESSION array that your application is liable to produce.
date_createdThis is used to identify when the session was started.
last_updatedThis is used to identify when the last request was processed for the session. This is also used in garbage collection to remove those sessions which have been inactive for a period of time.
user_idThis is used to identify the person to whom this session belongs. The value is provided by the application logon screen.


Tutorial Pages:
» Introduction
» Define database table
» Define database class
» Define session handler
» Conclusion


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1

Ask A Question
characters left.