GithubHelp home page GithubHelp logo

drolean / php-session Goto Github PK

View Code? Open in Web Editor NEW

This project forked from josantonius/php-session

1.0 2.0 0.0 1.11 MB

PHP library for handling sessions

Home Page: https://josantonius.com

License: MIT License

PHP 100.00%

php-session's Introduction

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($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
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 😉

You can contact me on Twitter or through my email.

php-session's People

Contributors

chrisrowley14 avatar josantonius avatar peter279k avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.