承接 tflori/nb-sessions 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tflori/nb-sessions

最新稳定版本:v2.0.2

Composer 安装命令:

composer require tflori/nb-sessions

包简介

Non-Blocking-Sessions made easy

README 文档

README

Build Status Coverage Status Latest Stable Version Total Downloads License

A non-blocking session handler for PHP. This library is inspired by duncan3dc/sessions.

A word on non-blocking sessions

Sessions are by default blocking in php. And that is for a good reason: to prevent race conditions. When a session is started by one request (to authenticate the user for example) another simultaneous request for this session has to wait till the session is released. This way all what got written in the first request is available in the second request.

When creating a custom session handler (a class that is handling session data). You can omit the locking mechanism and some default handlers don't support locking (ini setting session.save_handler). Why that is not a solution and this library is: session data is only read on session start and written at the end of the request.

When a second request comes in for the same session it reads the current state and writes the current state. Before it writes to the session it reads the current data by (re-)starting the session. Of course one issue is left: it does not read the data before each read. You should not rely on the data but a counter for example might require the session to be refreshed before updating: $session->counter = $session->refresh()->get('counter', 0) + 1.

Examples

basic

$session = new \NbSessions\SessionInstance();
$session->set('login', 'jdoe');
$login = $session->login;

namespaces

To avoid key collisions you can use namespaces.

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

$namespace = $session->getNamespace('my-module');
$namespace->set('foo', 'baz');

$session->foo; // 'bar'
$namespace->foo; // 'baz'

static class

For easier access you can use the static class. But remember: it's harder to test.

$namespace = \NbSessions\Session::getNamespace('my-module');
\NbSessions\Session::get('foo');

Setup

Install it via composer and use without configuration.

composer require tflori/nb-sessions

Options

The options are also taken from the php configuration. However, for convenience you can also pass them like that:

$session = new \NbSessions\SessionInstance([
    'name' => 'PHPSESSID', // defaults to ini_get('session.name')
    'cookie_lifetime' => 0, // in seconds; defaults to ini_get('session.cookie_lifetime')
    'cookie_path' => '/', // ...
    'cookie_domain' => '',
    'cookie_secure' => false, // if you serving via ssl only set this to true
    'cookie_httponly' => false, // we highly recommend this to be true
    'cookie_samesite' => '',
    'destroyEmpty' => true, // when enabled the session gets automatically destroyed once the last element got deleted
]);

Hints

This library does not overwrite the session configuration from php nor the registered session handler. All it does is handling and forcing the use of cookies and writing the session each time after something got changed.

The session will not be started before something is stored or a cookie is provided and an element is read. This prevents the application from starting useless requests to whatever session handler you are using.

As cookies will be handled by this library you don't have to worry about any cookies. It removes the cookie once destroyed and updates the cookie when the lifetime is not 0 (0 means the cookie is stored only for the current browser session).

统计信息

  • 总下载量: 3.64k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2017-02-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固