PHP Classes

ATK4 Session: Store and retrieve PHP session data in a database

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-06-03 (-4 hours ago) RSS 2.0 feedNot yet rated by the usersTotal: 96 All time: 9,863 This week: 43Up
Version License PHP version Categories
atk4-session 1.0.1MIT/X Consortium ...5PHP 5, User Management
Description 

Author

This package can be used to store and retrieve PHP session data in a database.

It provides a replacement for the PHP default session handler, so session data for each user that accesses a application handled by scripts that use PHP sessions.

The package can connect to a given database server, create the necessary database table to store session data, as well override some PHP session configuration options like the time to expire sessions and remove expired sessions.

Picture of Francesco Danti
  Performance   Level  
Name: Francesco Danti <contact>
Classes: 8 packages by
Country: Italy Italy
Innovation award
Innovation award
Nominee: 3x

Documentation

ATK4 Session Handler

License Maintainability Maintainability Technical Debt Test Coverage PHP version

Session handler for Atk4\Data (@see https://github.com/atk4/data)

Install

composer require abbadon1334/atk4-session

Initialize without atk4\ui


// autoload
include '../vendor/autoload.php';

// create pesistence
$db = \Atk4\data\Persistence::connect('mysql://root:password@localhost/atk4');

// init session handler
new \Atk4\ATK4DBSession\SessionHandler($p, [/session options/]);

Create session table using atk4\schema

(new \Atk4\Data\Schema\Migrator(new \atk4\ATK4DBSession\SessionModel($p)))->create();

OR

Create session table with SQL query

CREATE TABLE `session` (
  `id` bigint() NOT NULL AUTO_INCREMENT,
  `session_id` varbinary(255) DEFAULT NULL,
  `data` blob,
  `created_on` datetime(6) NULL DEFAULT NULL,
  `updated_on` datetime(6) NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `session_id` (`session_id`)
) ENGINE=InnoDB;

Session GC

if you use InnoDB deletes are slow, so the best option for huge request application is to set a cronjob which run every 2 minutes, you can find an example in : demos/example/cronjob.example.php. When you instantiate the SessionHandler, if you use the crojob, set the gc_probability option to 0 to disable automatic triggering of gc.

Why i need to replace the default PHP Session Handler with this?

Because of file locking ( here a good article about the argument link)

Every call that use sessions read a file and set a lock on it until release or output, to prevent race conditions.

It's clearly a shame to have file locking on things that are usually static, like nowadays sessions.

Using an alternative you'll have for sure race conditions, BUT what race condition can be if you, usually, have only an ID in $_SESSION and that is nearly immutable from login to logout.

SessionHandler will substitute SessionHandler class in PHP and will store session data in database using atk4\data instead of using files.

In atk4\ui where async calls are massively used, this problem is much more evident.

You can add it without breaking your project, it already works, but is still in development and need a strong review for security issue.


  Files folder image Files (25)  
File Role Description
Files folder image.github (1 file, 1 directory)
Files folder imagedemos (4 files, 2 directories)
Files folder imagesrc (2 files)
Files folder imagetests (4 files)
Files folder imagetools (1 file)
Accessible without login Plain text file .php-cs-fixer.dist.php Example Example script
Accessible without login Plain text file codecov.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpstan.neon.dist Data Auxiliary data
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file renovate.json Data Auxiliary data

  Files folder image Files (25)  /  .github  
File Role Description
Files folder imageworkflows (3 files)
  Accessible without login Plain text file release-drafter.yml Data Auxiliary data

  Files folder image Files (25)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file build-docs.yml Data Auxiliary data
  Accessible without login Plain text file build-release.yml Data Auxiliary data
  Accessible without login Plain text file test-unit.yml Data Auxiliary data

  Files folder image Files (25)  /  demos  
File Role Description
Files folder imageexample (1 file)
Files folder image_demo-data (1 file)
  Accessible without login Plain text file db.default.php Example Example script
  Plain text file init-autoloader.php Class Class source
  Accessible without login Plain text file init-db.php Example Example script
  Plain text file init-unit-tests.php Class Class source

  Files folder image Files (25)  /  demos  /  example  
File Role Description
  Accessible without login Plain text file cronjob.example.php Example Example script

  Files folder image Files (25)  /  demos  /  _demo-data  
File Role Description
  Accessible without login Plain text file create-db.php Example Example script

  Files folder image Files (25)  /  src  
File Role Description
  Plain text file SessionHandler.php Class Class source
  Plain text file SessionModel.php Class Class source

  Files folder image Files (25)  /  tests  
File Role Description
  Plain text file BaseTestCase.php Class Class source
  Accessible without login Plain text file router.php Example Example script
  Plain text file SessionHandlerCallTracer.php Class Class source
  Plain text file SessionHandlerTest.php Class Class source

  Files folder image Files (25)  /  tools  
File Role Description
  Plain text file CoverageUtil.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:96
This week:0
All time:9,863
This week:43Up