Support
- Advanced Backup
- Client Side
- Cloud Enterprise
- ColdFusion
- Control Panel
- cPanel
- Customer Portal
- DNS Information
- Dedicated Servers
- DirectAdmin
- Domain Name
- dotDefender
- Dreamweaver
- FileCatalyst
- Front Page
- FTP
- General Information
- Hosted Exchange & SharePoint
- IIS6
- IIS7
- Juniper Netscreen Firewalls
- Linux
- List Server
- MIVA Merchant
- MySQL
- Patching / Server Updates
- phpMyAdmin
- Plesk
- Policies and Procedures
- Premium Spam Filtering
- Programming
- 500 Error
- Accessdb 1
- Accessdb 2
- Aspmail
- Aspsqlinjection
- Aspupload
- Connectionstrings
- Faq Windows
- Locked Db
- Programming
- Use a JSP (Java) Redirect Script
- Conditional 301 Redirect
- Using ADOdb to Build a Database Agnostic PHP Application
- Store PHP Session Data in a Database with ADODB
- Use JQueryUI Sortable to Manipulate Sort Order in a Database Table
- Use PHP GD to Resize Images on Upload
- Add a Facebook Like Button to Your Website
- Add a Twitter Tweet Button to Your Website
- Enhance User Experience with JavaScript Form Validation
- Get Started Integrating FaceBook with the PHP-SDK
- Get Started with the Twitter API
- Ruby on Rails
- Search Engine Submission
- SharePoint 3
- SharePoint 2010
- SiteDesigner
- SmarterMail 3
- SmarterMail 4
- SmarterMail 5
- SmarterMail 6
- SmarterMail 7
- SmarterStats
- SmarterTrack
- SQL Server
- Secure Socket Layer (SSL)
- Uploading Your Website
- Video Tutorials
- Windows Server 2003
- Windows Server 2008
- Web Design
- WordPress
- Advanced Monitoring
- MediaWiki
- Enkompass
- Microsoft Outlook 2010
- Android
- Outlook Web Access
- Critical Availability Service
- NAS Data Transfer
- Customer Portal Demos
- Joomla
- Moodle
- Cloud Dedicated
- Gallery CMS
- phpBB
- Standard Monitoring
- Righteous Restore
- NAS (Network Attached Storage)
- Networking
- SmarterMail 8
- PCI Security Scan
- LinkTiger
- Windows Cloud VPS
- Linux Cloud VPS
- Linux VPS
- Windows VPS
- Hyper V
- ENSIM
- Alert Logic
- Webmin
- e107
- Vbulletin
- VPN
- Visual Vault
- Mozilla Thunderbird
- PyroCMS
- Active Directory
- Vmware Related
- Drupal
Store PHP Session Data in a Database with ADODB
Storing session data in a database is a great way to plan for scaling your application into a load balanced architecture.
The use of session data is common in websites and applications. Typically a cookie is set in the client's browser that refers to data being stored on a server. This extends a website beyond the stateless model the HTTP protocal is based on. Storing information from a previous request allows applications to "remember" what the end user did. This allows your website to handle things like login status.
When your website grows beyond the capacity of a single server, changing the way session data is handled should be considered. In a load balanced configuration you don't want your session data to be lost when any one server becomes inaccessible. Storing your sessions in a database is a great way to handle it. This creates separation between your application servers and the location of your session data. Separating this data allows you to load balancing your servers without fear of a poor user expereince when a single instance is removed from the cluster. Using a database is also more secure than the traditional file based method open to unscrupulous attack.
Luckily ADODB offers a very easy way to move your PHP session data into a database.
Refer to Building a Database Agnostic PHP Application with ADODB for the basic installation instructions.
Create the Database Table to Store your Session Data
For MySql run the command below.
CREATE TABLE sessions2(
sesskey VARCHAR( 64 ) NOT NULL DEFAULT '',
expiry DATETIME NOT NULL ,
expireref VARCHAR( 250 ) DEFAULT '',
created DATETIME NOT NULL ,
modified DATETIME NOT NULL ,
sessdata LONGTEXT,
PRIMARY KEY ( sesskey ) ,
INDEX sess2_expiry( expiry ),
INDEX sess2_expireref( expireref )
)
Configure the Application
Include the adodb-session2.php file to make the session management classes available.
require_once("adodb/session/adodb-session2.php");
Configure the session before starting the session.
ADOdb_Session::config($driver, $host, $user, $password, $database,$options);
$ADODB_SESSION_EXPIRE_NOTIFY = array('userid','NotifyFn');
function NotifyFn($userid,$sessionkey){
#this routine is processed at session expiration
};
Session Usage
Start the session
session_start();
Login user 12345
$_SESSION['userid'] = 12345;
Logout user 12345
unset($_SESSION['userid']);
