PHP library for handling sessions.
- Requirements
- Installation
- Available Methods
- Quick Start
- Usage
- Tests
- TODO
- Contribute
- Repository
- License
- Copyright
This library is supported by PHP versions 5.6 or higher.
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
Or install it manually:
$ wget https://raw.githubusercontent.com/Josantonius/PHP-Session/master/src/Session.php
Available methods in this library:
Session::setPrefix($prefix);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$prefix | Prefix for sessions. | object | Yes |
# Return (boolean)
Session::getPrefix();
# Return (string) → sessions prefix
Session::init($lifeTime);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$lifeTime | Life time during session. | int | No | 0 |
# Return (boolean)
Session::set($key, $value);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$key | Session name. | string | Yes | |
$value | The data to save. | mixed | No | false |
# Return (boolean true)
Session::pull($key);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$key | Item to extract. | string | Yes |
# Return (mixed|null) → return item or null when key does not exists
Session::get($key, $secondkey);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$key | Item to look for in session. | string | No | '' |
$secondkey | If used then use as a second key. | string | boolean | No |
# Return (mixed|null) → return item or null when key does not exists
Session::id();
# Return (string) → the session id or empty
Session::regenerate();
# Return (string) → the new session id
Session::destroy($key, $prefix);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$key | Session ID to destroy. | string | No | '' |
$prefix | If true clear all sessions for current prefix. | boolean | No | false |
# Return (boolean)
To use this library with Composer:
require __DIR__ . '/vendor/autoload.php';
use Josantonius\Session\Session;
Or If you installed it manually, use it:
require_once __DIR__ . '/Session.php';
use Josantonius\Session\Session;
Example of use for this library:
Session::setPrefix('_prefix');
Session::getPrefix();
Session::init();
Session::init(3600);
Session::set('name', 'Joseph');
$data = [
'name' => 'Joseph',
'age' => '28',
'business' => ['name' => 'Company'],
];
Session::set($data);
Session::pull('age');
Session::get('name');
Session::get('business', 'name');
Session::get();
Session::id();
Session::regenerate();
Session::destroy('name');
Session::destroy('ses_', true);
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 PSR2 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.
- Refactor code for disabled code style rules. See phpmd.xml and .php_cs.dist.
- Add tests for session duration in the init() method.
If you would like to help, please take a look at the list of issues or the To Do checklist.
Pull requests
- Fork and clone.
- Run the command
composer install
to install the dependencies. This will also install the dev dependencies. - Run the command
composer fix
to excute code standard fixers. - Run the tests.
- Create a branch, commit, push and send me a pull request.
Thank you to all the people who already contributed to this project!
peter279k | chrisrowley14 |
The file structure from this repository was created with PHP-Skeleton.
This project is licensed under MIT license. See the LICENSE file for more info.
2017 - 2018 Josantonius, josantonius.com
If you find it useful, let me know 😉