PHP Classes
elePHPant
Icontem

PHP PostgreSQL Session Handler: Store PHP sessions in PostgreSQL table using PDO

Recommend this page to a friend!
  Info   View files Documentation   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-04-22 (5 months ago) RSS 2.0 feedNot enough user ratingsTotal: 116 This week: 1All time: 8,654 This week: 631Up
Version License PHP version Categories
pgsessions 1.0.0MIT/X Consortium ...5PHP 5, User Management
Description Author

This class implements a session handler that store PHP sessions in PostgreSQL table using PDO.

It takes a PDO connection object to access a PostgreSQL database that can perform the PHP session handling functions to store and retrieve session data in a table of a database accessible via the specified PDO object.

The class object can be assigned as the current PHP session save handler.

Innovation Award
PHP Programming Innovation award nominee
May 2017
Number 10
PHP usually stores session data in files but it also allows to define custom session handler classes that can store session data in other types of container.

This class implements a session handler that stores data in a database. Despite the most used database with PHP is MySQL, PostgreSQL is also very popular.

This class supports storing session data in PostgreSQL database using PDO.

Manuel Lemos
Name: Kevin Shah <contact>
Classes: 1 package by
Country: India India
Age: ???
All time rank: 4043282 in India India
Week rank: 515 Up43 in India India Up
Innovation award
Innovation award
Nominee: 1x

Details

Saving PHP sessions in Postgres Database.

> Disclaimer: Storing sessions in PHP via files(default) and other caching systems like Memcache / memcached / Redis is definitely faster but there was this use case where I had to use RDBMS for storing sessions and hence this. Use this only when it is a strict requirement to save sessions in a relational database

Single file (PGSessions.php)

Requirements:

  • PostgreSQL 9.5+ [Dependency on using UPSERT]
  • PHP 7.0+ (Might be compatible with 5.4 plus but not tested)
  • \PDO Connected Database Object
  • Create "sessions" database as provided below.

    CREATE TABLE "sessions" ( "id" TEXT NOT NULL UNIQUE, "last_updated" BIGINT NOT NULL, "expiry" bigint NOT NULL, "data" TEXT NOT NULL); CREATE INDEX "valid_sessions" ON "sessions"("id"); CREATE INDEX "nonexpired_sessions" ON "sessions"("id","expiry");

Steps to use:

  • Pass the PDO Connected to the class constructor.
  • Define SESSION_DURATION constants to set session expiry.

    SESSION_DURATION -> Expiry of sessions in number of seconds.

Demo


require_once 'PGSessions.php';

$pdo_connection = new PDO(...);

use \PGSessions\PGSessions;
$sessions_handler = new PGSessions($pdo_connection);
session_set_save_handler($sessions_handler, true);
session_name('MySessionName');
session_start();
session_regenerate_id(true);

$_SESSION['something'] = 'foo';
$_SESSION['something_else'] = 'bar';
echo session_id(),'<br/>',$_SESSION['something'],'<br />',$_SESSION['something_else'] ;
/*
Rest of your script
.
.
.
.
.
.
.
.
.
.
Before ending your script
*/
session_write_close();
//Note that session_write_close is not REQUIRED to be called on each page since we register_shutdown_function=true (second parameter = true in  session_set_save_handler).

  Files folder image Files  
File Role Description
Plain text file PGSessions.php Class Class source
Accessible without login Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:116
This week:1
All time:8,654
This week:631Up