PHP library for handling sessions

Overview

Total Downloads Latest Stable Version License

Horizom Session

PHP library for handling sessions.


Requirements

This library is compatible with the PHP versions: 8.0 | 8.1.

Installation

The preferred way to install this extension is through Composer.

To install PHP Session library, simply:

composer require horizom/session

The previous command will only install the necessary files, if you prefer to download the entire source code you can use:

composer require horizom/session --prefer-source

Available Methods

Available methods in this library:

Starts the session

$session->start(array $options = []);

@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

@Return bool

Check if the session is started

$session->isStarted();

@Return bool

Sets an attribute by name

$session->set(string $name, mixed $value = null);

@throws SessionException If session is unstarted

@Return void

Gets an attribute by name

Optionally defines a default value when the attribute does not exist.

$session->get(string $name, mixed $default = null);

@Return mixed Value

Gets all attributes

$session->all();

@Return array $_SESSION content

Check if an attribute exists in the session

$session->has(string $name);

@Return bool

Sets several attributes at once

If attributes exist they are replaced, if they do not exist they are created.

$session->replace(array $data);

@throws SessionException If session is unstarted

@Return void

Deletes an attribute by name and returns its value

Optionally defines a default value when the attribute does not exist.

$session->pull(string $name, mixed $default = null);

@throws SessionException If session is unstarted

@Return mixed Attribute value

Deletes an attribute by name

$session->remove(string $name);

@throws SessionException If session is unstarted

@Return void

Free all session variables

$session->clear();

@throws SessionException If session is unstarted

@Return void

Gets the session ID

$session->getId();

@Return string Session ID

Sets the session ID

$session->setId(string $sessionId);

@throws SessionException If session already started

@Return void

Update the current session id with a newly generated one

$session->regenerateId(bool $deleteOldSession = false);

@throws SessionException If session is unstarted

@Return bool

Gets the session name

$session->getName();

@Return string Session name

Sets the session name

$session->setName(string $name);

@throws SessionException If session already started

@Return void

Destroys the session

$session->destroy();

@throws SessionException If session is unstarted

@Return bool

Quick Start

To use this library with Composer:

use Horizom\Session\Session;

$session = new Session();

Or instead you can use a facade to access the methods statically:

use Horizom\Session\Facades\Session;

Usage

Example of use for this library:

- Starts the session

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,
]);

Using the facade:

Session::start();

- Check if the session is started

Using session object:

$session->isStarted();

Using the facade:

Session::isStarted();

- Sets an attribute by name

Using session object:

$session->set('foo', 'bar');

Using the facade:

Session::set('foo', 'bar');

- Gets an attribute by name

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

Using the facade:

Session::get('foo');

- Gets all attributes

Using session object:

$session->all();

Using the facade:

Session::all();

- Check if an attribute exists in the session

Using session object:

$session->has('foo');

Using the facade:

Session::has('foo');

- Sets several attributes at once

Using session object:

$session->replace(['foo' => 'bar', 'bar' => 'foo']);

Using the facade:

Session::replace(['foo' => 'bar', 'bar' => 'foo']);

- Deletes an attribute by name and returns its value

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

Using the facade:

Session::pull('foo');

- Deletes an attribute by name

Using session object:

$session->remove('foo');

Using the facade:

Session::remove('foo');

- Free all session variables

Using session object:

$session->clear();

Using the facade:

Session::clear();

- Gets the session ID

Using session object:

$session->getId();

Using the facade:

Session::getId();

- Sets the session ID

Using session object:

$session->setId('foo');

Using the facade:

Session::setId('foo');

- Update the current session id with a newly generated one

Regenerate ID without deleting the old session:

$session->regenerateId();

Regenerate ID by deleting the old session:

$session->regenerateId(true);

Using the facade:

Session::regenerateId();

- Gets the session name

Using session object:

$session->getName();

Using the facade:

Session::getName();

- Sets the session name

Using session object:

$session->setName('foo');

Using the facade:

Session::setName('foo');

- Destroys the session

Using session object:

$session->destroy();

Using the facade:

Session::destroy();

License

This repository is licensed under the MIT License.

You might also like...
Icinga Director has been designed to make Icinga 2 configuration handling easy
Icinga Director has been designed to make Icinga 2 configuration handling easy

The Director aims to be your new favourite Icinga config deployment tool. Director is designed for those who want to automate their configuration deployment and those who want to grant their “point & click” users easy access to the configuration.

Easy to use utility functions for everyday PHP projects. This is a port of the Lodash JS library to PHP

Lodash-PHP Lodash-PHP is a port of the Lodash JS library to PHP. It is a set of easy to use utility functions for everyday PHP projects. Lodash-PHP tr

PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP language

php-text-analysis PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP l

php-echarts is a php library for the echarts 5.0.

php-echarts 一款支持Apache EChart5.0+图表的php开发库 优先ThinkPHP5/6的开发及测试。 Apache EChart5.0已经最新发布,在视觉效果、动画效果和大数据展示方面已经远超之前的版本; 故不考虑EChart5.0之前版本的兼容问题;建议直接尝试5.0+

Minimalist PHP frame for Core-Library, for Developing PHP application that gives you the full control of your application.

LazyPHP lightweight Pre-Made Frame for Core-library Install Run the below command in your terminal $ composer create-project ryzen/lazyphp my-first-pr

Gettext is a PHP (^7.2) library to import/export/edit gettext from PO, MO, PHP, JS files, etc.

Gettext Note: this is the documentation of the new 5.x version. Go to 4.x branch if you're looking for the old 4.x version Created by Oscar Otero http

Columnar analytics for PHP - a pure PHP library to read and write simple columnar files in a performant way.

Columnar Analytics (in pure PHP) On GitHub: https://github.com/envoymediagroup/columna About the project What does it do? This library allows you to w

:date: The VObject library for PHP allows you to easily parse and manipulate iCalendar and vCard objects

sabre/vobject The VObject library allows you to easily parse and manipulate iCalendar and vCard objects using PHP. The goal of the VObject library is

Releases(3.1.0)
Owner
Horizom
A light PHP Framework
Horizom
MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query and get result in a fastest way

Mysql Optimizer mysql optimizer also known as MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query

null 2 Nov 20, 2021
Firebird-PHP: A library created to meet a work need when handling a Firebird database

Firebird-PHP: A library created to meet a work need when handling a Firebird database

Philipe  Lima 3 Jun 27, 2022
html-sanitizer is a library aiming at handling, cleaning and sanitizing HTML sent by external users

html-sanitizer html-sanitizer is a library aiming at handling, cleaning and sanitizing HTML sent by external users (who you cannot trust), allowing yo

Titouan Galopin 381 Dec 12, 2022
Receiver is a drop-in webhook handling library for Laravel.

Receiver Receiver is a drop-in webhook handling library for Laravel. Webhooks are a powerful part of any API lifecycle. Receiver aims to make handling

Adam Campbell 270 Jan 6, 2023
Decimal handling as value object instead of plain strings.

Decimal Object Decimal value object for PHP. Background When working with monetary values, normal data types like int or float are not suitable for ex

Spryker 16 Oct 24, 2022
Composer script handling your ignored parameter file

Managing your ignored parameters with Composer This tool allows you to manage your ignored parameters when running a composer install or update. It wo

Incenteev 921 Nov 21, 2022
QueryHandler - Handling PDO ' s query with mySQL database

QueryHandler this class's method are static .... that mean you don't need to create an object to use it . All methodes will return an Exception if it

null 7 Aug 9, 2022
PocketMine-MP virion for easy handling of ScoreBoard packets

PocketMine-MP virion for easy handling of ScoreBoard packets

pocketmine virions of avas 3 Apr 9, 2022
A XOOPS module for handling events, including online registrations.

wgEvents A XOOPS module for handling events, including online registrations. Support If you like the wgEvents module and thanks to the long process fo

XOOPS 2.5.x Modules 6 Dec 15, 2022
An opinionated extension package for Laravel Orchid to extend its table handling capabilities, and some further useful helper methods.

OrchidTables An opinionated extension package for Laravel Orchid to extend its table handling capabilities, and some further useful helper methods. In

null 25 Dec 22, 2022