PHP Classes
elePHPant
Icontem

File: README.md

Recommend this page to a friend!
  Classes of Josantonius  >  PHP Session Manager  >  README.md  >  Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP Session Manager
Manage values stored as session variables
Author: By
Last change: Updated to 1.1.7 version
Updated to 1.1.7 version
Date: 1 year ago
Size: 8,041 bytes
 

 

Contents

Class file image Download

PHP Session library

Latest Stable Version Latest Unstable Version License Codacy Badge Total Downloads Travis PSR2 PSR4 CodeCov

Versión en español

PHP library for handling sessions.

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:

Download Session.php:

$ 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();

# 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 | false |

# 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::set('name', 'Joseph');

- Get sessions prefix:

Session::getPrefix();

- Start session:

Session::init();

- Add value to a session:

Session::init();

- 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.

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!

<img alt="peter279k" src="https://avatars2.githubusercontent.com/u/9021747?v=4&s=117" height="117" width="117"> | :---:| peter279k|

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 :wink:

You can contact me on Twitter or through my email.