josantonius / session
PHP library for handling sessions.
Requires
- php: ^5.6 || ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.3 || ^2.8
- phpmd/phpmd: ^2.6
- phpunit/phpunit: ^5.7 || ^6.0
- squizlabs/php_codesniffer: ^3.0
README
PHP library for handling sessions.
- Requirements
- Installation
- Available Methods
- Quick Start
- Usage
- Tests
- TODO
- Contribute
- Repository
- License
- Copyright
Requirements
This library is supported by PHP versions 5.6 or higher.
Installation
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
Available methods in this library:
- Set prefix for sessions:
Session::setPrefix($prefix);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$prefix | Prefix for sessions. | object | Yes |
# Return (boolean)
- Get sessions prefix:
Session::getPrefix();
# Return (string) → sessions prefix
- Start session if session has not started:
Session::init($lifeTime);
Attribute | Description | Type | Required | Default |
---|---|---|---|---|
$lifeTime | Life time during session. | int | No | 0 |
# Return (boolean)
- Add value to a session:
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)
- Extract session item, delete session item and finally return the item:
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
- Get item from session:
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
- Get session id:
Session::id();
# Return (string) → the session id or empty
- Regenerate session_id:
Session::regenerate();
# Return (string) → the new session id
- Empties and destroys the session:
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)
Quick Start
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;
Usage
Example of use for this library:
- Set prefix for sessions:
Session::setPrefix('_prefix');
- Get sessions prefix:
Session::getPrefix();
- Start session:
Session::init();
- Start session by setting the session duration:
Session::init(3600);
- Add value to a session:
Session::set('name', 'Joseph');
- Add multiple value to sessions:
$data = [ 'name' => 'Joseph', 'age' => '28', 'business' => ['name' => 'Company'], ]; Session::set($data);
- Extract session item, delete session item and finally return the item:
Session::pull('age');
- Get item from session:
Session::get('name');
- Get item from session entering two indexes:
Session::get('business', 'name');
- Return the session array:
Session::get();
- Get session id:
Session::id();
- Regenerate session_id:
Session::regenerate();
- Destroys one key session:
Session::destroy('name');
- Destroys sessions by prefix:
Session::destroy('ses_', true);
- Destroys all sessions:
Session::destroy();
Tests
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
☑ TODO
- 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.
Contribute
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 |
Repository
The file structure from this repository was created with PHP-Skeleton.
License
This project is licensed under MIT license. See the LICENSE file for more info.
Copyright
2017 - 2018 Josantonius, josantonius.com
If you find it useful, let me know 😉