phputil/session

A simple object-oriented wrapper to PHP's session functions.

2.1.1 2017-05-05 07:46 UTC

This package is auto-updated.

Last update: 2024-10-23 04:39:08 UTC


README

PHP's session in a object-oriented version.

Main files:

This project uses semantic versioning.

Installation

composer require phputil/session

Example 1

Setting and getting a value from the session.

$session = new phputil\FileBasedSession();
$session->start();

$session->put( 'user_name', $_POST[ 'user_name' ] ); // Set a value in the session
echo 'Hello, ', $session->get( 'user_name' ); // Get a value from the session

Example 2

Setting session cookie name and cookie duration.

$session = new phputil\FileBasedSession();
$session->setName( 'myapp' ); // (optional) "PHPSESSID" session cookie key becomes "myapp"
$session->setCookieParams( $lastOneDay = 60 * 60 * 24 ); // (optional) cookie will last one day
$session->start();

Example 3

Swapping between sessions.

$session = new phputil\FileBasedSession();

$session->start();
$savedId = $session->id();
$session->close();

// Opening another session
$session->id( $_GET[ 'another_id' ] );
$session->start(); // starts the session with id "another_id"
...
$session->close();

// Restoring the session with "savedId"
$session->id( $savedId );
$session->start();

Example 4

Regenerating the session id.

$session = new phputil\FileBasedSession();
$session->start();
$session->regenerateId( true ); // true means delete the old file

Example 5

Destroying the session.

$session = new phputil\FileBasedSession();
$session->start();
$session->destroy();