2017 © Pedro Peláez
 

library session

Manage session state without native PHP sessions

image

wolfmoritz/session

Manage session state without native PHP sessions

  • Sunday, March 26, 2017
  • by wolfmoritz
  • Repository
  • 2 Watchers
  • 0 Stars
  • 49 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 2 % Grown

The README.md

Archived

Project has been moved to https://github.com/PitonCMS/Session where it being maintained., (*1)

PHP Session Handler

This class maintains session state across page views. A hashed, salted session key is set in a cookie which is the key to the session record in a MySQL table. The session key is regenerated every 5 minutes or as set in the configuation array. No information, other than the key, is stored client side. All user session information is kept server side in a database table., (*2)

When the session runs the session handler looks for a session record matching the cookie key, and if found it then runs optional checks to validate the session. If any of the checks fail, or if the session has timed out, the session is destoyed and a new session is started., (*3)

Session data can be set and retrieved at any time as either a key-value pair, or an array of key-value pairs. Flash data is also supported, which only persists until the next request., (*4)

Installation

You can use Composer to install the session handler or just download the files to your project., (*5)

Using Composer

Either require the project in composer,, (*6)

composer require wolfmoritz/session

or modify your composer.json project file to require this package and run an update or install., (*7)

"require": {
  "wolfmoritz/session": "~1.2.0"
}

This will download the files and register the class with the composer autoloader., (*8)

Or Just the Files, Please

If you do not use Composer, download this project and unzip. The only file you need is src/Session/SessionHandler.php. Place that file in your project and be sure to include it in your startup script., (*9)

Create the Table

You will need to create the session table in your MySQL database using the SessionTable.sql script. You can change the table name in the script if desired, but you will need to provide the table name as a configuration item for tableName., (*10)

Usage

To use the session handler create a new instance of WolfMoritz\Session\SessionHandler passing in a PDO database connection and the configuration array., (*11)

Provide a PDO Connection

Define a new PDO connection and pass it in as the first argument of the constructor., (*12)

$dsn = 'mysql:host=' . HOST . ';dbname=' . DBNAME;
$dbh = new PDO($dsn, USERNAME, PASSWORD, DB_OPTIONS);

Define Configuruation

Define a configuration array and only include the options you wish to change. The only required configuration option is your application's encryption key. Use a long, random string and do not share it., (*13)

Options, (*14)

Option Default Description
cookieName 'sessionCookie' Your session cookie name.
tableName 'session' Name of the MySQL table that stores your sessions.
secondsUntilExpiration 7200 How long before the session expires in seconds.
renewalTime 300 How long before the session key is regenerated in seconds.
expireOnClose false Whether or not to destroy the session when the browser closes, true or false.
checkIpAddress false Whether or not to match the IP address to the stored session, true or false.
checkUserAgent false Whether or not to match the User Agent to the stored session, true or false.
secureCookie false Whether or not to set an encrypted cookie, true of false. Note, this only works when using https.
salt none Your custom encryption key. Any long (16+ characters) string of characters.
autoRunSession true Whether to run the session automatically, or only needed.
$config['salt'] = 'akjfao8ygoa8hba9707lakusdof87'; // Use your own salt!
$config['secondsUntilExpiration'] = 1800; // 30 minutes
// More configuration options ...

Creating a Session

Create a new session as part of your application flow., (*15)

$Session = new WolfMoritz\Session\SessionHandler($dbh, $config);

The session runs immediately if autoRunSession is set to true (which is the default) and checks for a valid session, regenerates the session key if necessary, and loads any existing session data for immediate retrieval. Create the session object once, or simply add it to your Dependency Injection Container as a singleton., (*16)

When the session runs, it queries the database. If you need to make the session class available but do not need to immediately run the session you can set autoRunSession to false in the configuration. Then when you are ready to run a session call the run() method., (*17)

$Session->run();

Save Session Data

Once the session is running, you can add or update session data by passing in key-value pairs or an array of key-value pairs using the setData() method. The value can be any type as long as it is serializable to JSON., (*18)

// Save simple key-value pair
$Session->setData('lupine', 'wolf');

// Save array of key-value pairs
$sessionData = array(
  'feline' => 'cat',
  'canine' => 'dog',
  'lastModified' => 1422592486
);
$Session->setData($sessionData);

Supplying the same key will overwrite any prior value saved in session., (*19)

Get Session Data

To get session data pass in the item key to getData(). The method returns null if no key was found. To get all session data simply do not provide an argument., (*20)

// Get one session item
$value = $Session->getData('keyName');

// Get all session items
$values = $Session->getData();

Save Flash Data

You can add flash data by passing in a string, a key-value pair, or an array of key-value pairs using the setFlashData() method. The value can be any type as long as it is serializable to JSON. Flash data will only stay until the next request, after which it is removed automatically., (*21)

// Save simple key-value pair
$Session->setFlashData('Something went wrong!');
$Session->setFlashData('alert', 'Something went very wrong!');

Get Flash Data

To get flash data pass in the item key to getFlashData(). The method returns null if no key was found. To get all flash data simply do not provide an argument. Flash data is automatically removed on the next request, so there's no need to explicitly unset flash data., (*22)

// Get one flash item
$value = $Session->getFlashData('alert');

// Get all flash items
$values = $Session->getFlashData();

Unset Session Data

You can delete a session item by passing in the item key to unsetData(), or delete all session data by not providing an argument., (*23)

// Delete one session item
$Session->unsetData('keyName');

// Delete all session data
$Session->unsetData();

Delete a Session

Any session that does not pass validation will be destoyed automatically. But, if you want to delete a session call the destroy() method., (*24)

$Session->destroy();

WARNING

Use this session class at your own risk. Read the code, and understand what it does if you intend on using this for for anything secure. I make no warranty if something goes wonky., (*25)

But, if you have any improvments please fork this project and send me a pull request!, (*26)

The Versions

26/03 2017

dev-master

9999999-dev https://github.com/wolfmoritz/session

Manage session state without native PHP sessions

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Wolfgang Moritz

session

22/06 2016

1.2.0

1.2.0.0 https://github.com/wolfmoritz/session

Manage session state without native PHP sessions

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Wolfgang Moritz

session

06/04 2016

1.1.0

1.1.0.0 https://github.com/wolfmoritz/session

Manage session state without native PHP sessions

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Wolfgang Moritz

session

16/02 2015

1.0.1

1.0.1.0 https://github.com/wolfmoritz/session

Manage session state without native PHP sessions

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Wolfgang Moritz

session

30/01 2015

1.0.0

1.0.0.0 https://github.com/wolfmoritz/session

Manage session state without native PHP sessions

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Wolfgang Moritz

session