middlewares / php-session
Middleware to start php sessions using the request data
Installs: 17 029
Dependents: 4
Suggesters: 0
Security: 0
Stars: 14
Watchers: 2
Forks: 3
Open Issues: 1
Requires
- php: ^7.2 || ^8.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- laminas/laminas-diactoros: ^2.3
- middlewares/utils: ^3.0
- oscarotero/php-cs-fixer-config: ^1.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8|^9
- squizlabs/php_codesniffer: ^3.0
README
Middleware to start a php session using the request data and close it after returning the response. Reads and writes session cookies in the PSR-7 request/response.
Requirements
- PHP >= 7.2
- A PSR-7 http message implementation
- A PSR-15 middleware dispatcher
Installation
This package is installable and autoloadable via Composer as middlewares/php-session.
composer require middlewares/php-session
Example
Dispatcher::run([ new Middlewares\PhpSession(), function () { //Use the global $_SESSION variable to get/set data $_SESSION['name'] = 'John'; } ]);
Usage
This is a middleware to start the native PHP session using the cookies of the server request.
name
The session name. If it's not provided, use the php's default name (PHPSESSID). More info session_name
// Start the session with other name $session = (new Middlewares\PhpSession())->name('user_session');
id
This option set a session id. If it's not provided, use the request's cookies to get it.
// Start the session with a specific session id $session = (new Middlewares\PhpSession())->id('foo');
options
This allows to set an of options passed to session_start()
// Start the session with a specific session id $session = (new Middlewares\PhpSession())->options([ 'cookie_lifetime' => 86400 ]);
regenerateId
This option regenerates the id after a specific time interval. The latest regeneration time is saved in the key session-id-expires
but you can change it in the second argument:
// Regenerate the session id after 60 seconds $session = (new Middlewares\PhpSession())->regenerateId(60); // Regenerate the session id after 60 seconds, storing the expires date in the key 'expiresAt' $session = (new Middlewares\PhpSession())->regenerateId(60, 'expiresAt');
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.