Translations: Español
PHP library for handling sessions.
Version 1.x is considered as deprecated and unsupported. In this version (2.x) the library was completely restructured. It is recommended to review the documentation for this version and make the necessary changes before starting to use it, as it not be compatible with version 1.x.
- Requirements
- Installation
- Available Methods
- Quick Start
- Usage
- Tests
- TODO
- Changelog
- Contribution
- Sponsor
- License
This library is compatible with the PHP versions: 8.0 | 8.1.
The preferred way to install this extension is through Composer.
To install PHP Session library, simply:
composer require josantonius/session
The previous command will only install the necessary files, if you prefer to download the entire source code you can use:
composer require josantonius/session --prefer-source
You can also clone the complete repository with Git:
git clone https://github.com/josantonius/php-session.git
Available methods in this library:
$session->start(array $options = []): bool
@see https://php.net/session.configuration
for List of available $options
and their default values.
@throws SessionException
if headers already sent.
@throws SessionException
if session already started.
@throws SessionException
if setting options failed.
$session->isStarted(): bool
$session->set(string $name, mixed $value): void
@throws SessionException
if session is unstarted.
Optionally defines a default value when the attribute does not exist.
$session->get(string $name, mixed $default = null): mixed
$session->all(): array
$session->has(string $name);
If attributes exist they are replaced, if they do not exist they are created.
$session->replace(array $data): void
@throws SessionException
if session is unstarted.
Optionally defines a default value when the attribute does not exist.
$session->pull(string $name, mixed $default = null): mixed
@throws SessionException
if session is unstarted.
$session->remove(string $name): void
@throws SessionException
if session is unstarted.
$session->clear(): void
@throws SessionException
if session is unstarted.
$session->getId() string
$session->setId(string $sessionId): void
@throws SessionException
if session already started.
$session->regenerateId(bool $deleteOldSession = false): bool
@throws SessionException
if session is unstarted.
$session->getName(): string
$session->setName(string $name): void
@throws SessionException
if session already started.
$session->destroy(): bool
@throws SessionException
if session is unstarted.
To use this library with Composer:
use Josantonius\Session\Session;
$session = new Session();
Alternatively you can use a facade to access the methods statically:
use Josantonius\Session\Facades\Session;
Example of use for this library:
Without setting options:
$session->start();
Setting options:
$session->start([
// 'cache_expire' => 180,
// 'cache_limiter' => 'nocache',
// 'cookie_domain' => '',
'cookie_httponly' => true,
'cookie_lifetime' => 8000,
// 'cookie_path' => '/',
'cookie_samesite' => 'Strict',
'cookie_secure' => true,
// 'gc_divisor' => 100,
// 'gc_maxlifetime' => 1440,
// 'gc_probability' => true,
// 'lazy_write' => true,
// 'name' => 'PHPSESSID',
// 'read_and_close' => false,
// 'referer_check' => '',
// 'save_handler' => 'files',
// 'save_path' => '',
// 'serialize_handler' => 'php',
// 'sid_bits_per_character' => 4,
// 'sid_length' => 32,
// 'trans_sid_hosts' => $_SERVER['HTTP_HOST'],
// 'trans_sid_tags' => 'a=href,area=href,frame=src,form=',
// 'use_cookies' => true,
// 'use_only_cookies' => true,
// 'use_strict_mode' => false,
// 'use_trans_sid' => false,
]);
Session::start();
$session->isStarted();
Session::isStarted();
$session->set('foo', 'bar');
Session::set('foo', 'bar');
Without default value if attribute does not exist:
$session->get('foo'); // null if attribute does not exist
With default value if attribute does not exist:
$session->get('foo', false); // false if attribute does not exist
Session::get('foo');
$session->all();
Session::all();
$session->has('foo');
Session::has('foo');
$session->replace(['foo' => 'bar', 'bar' => 'foo']);
Session::replace(['foo' => 'bar', 'bar' => 'foo']);
Without default value if attribute does not exist:
$session->pull('foo'); // null if attribute does not exist
With default value if attribute does not exist:
$session->pull('foo', false); // false if attribute does not exist
Session::pull('foo');
$session->remove('foo');
Session::remove('foo');
$session->clear();
Session::clear();
$session->getId();
Session::getId();
$session->setId('foo');
Session::setId('foo');
Regenerate ID without deleting the old session:
$session->regenerateId();
Regenerate ID by deleting the old session:
$session->regenerateId(true);
Session::regenerateId();
$session->getName();
Session::getName();
$session->setName('foo');
Session::setName('foo');
$session->destroy();
Session::destroy();
To run tests you just need composer and to execute the following:
git clone https://github.com/josantonius/php-session.git
cd php-session
composer install
Run unit tests with PHPUnit:
composer phpunit
Run code standard tests with PHPCS:
composer phpcs
Run PHP Mess Detector tests to detect inconsistencies in code style:
composer phpmd
Run all previous tests:
composer tests
- Add new feature
- Improve tests
- Improve documentation
- Improve English translation in the README file
- Refactor code for disabled code style rules (see phpmd.xml and phpcs.xml)
- Show an example of renewing the session lifetime
- Feature to enable/disable exceptions?
- Feature to add prefixes in session attributes?
Detailed changes for each release are documented in the release notes.
Please make sure to read the Contributing Guide, before making a pull request, start a discussion or report a issue.
Thanks to all contributors! ❤️
If this project helps you to reduce your development time, you can sponsor me to support my open source work 😊
This repository is licensed under the MIT License.
Copyright © 2017-present, Josantonius