adbario/slim-secure-session-middleware 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

adbario/slim-secure-session-middleware

Composer 安装命令:

composer require adbario/slim-secure-session-middleware

包简介

Secure session middleware for Slim 3 framework

README 文档

README

Secure session middleware for Slim 3 framework.

  • longer and more secure session id's
  • session data encryption
  • set session cookie path, domain and secure values automatically
  • extend session lifetime after each user activity
  • helper class to handle session values easily with namespaces and dot notation (Dot - PHP dot notation array access)

If you're on shared host and use sessions for storing sensitive data, it's a good idea to store session files in your custom location and encrypt them.

Installation

Via composer:

composer require adbario/slim-secure-session-middleware

Configuration

Create an array of session settings in you application settings. Please note that session lifetime is defined in minutes, not in seconds. Default settings for session:

$settings = [
    'session' => [
        // Session cookie settings
        'name'           => 'slim_session',
        'lifetime'       => 24,
        'path'           => '/',
        'domain'         => null,
        'secure'         => false,
        'httponly'       => true,

        // Set session cookie path, domain and secure automatically
        'cookie_autoset' => true,

        // Path where session files are stored, PHP's default path will be used if set null
        'save_path'      => null,

        // Session cache limiter
        'cache_limiter'  => 'nocache',

        // Extend session lifetime after each user activity
        'autorefresh'    => false,

        // Encrypt session data if string is set
        'encryption_key' => null,

        // Session namespace
        'namespace'      => 'slim_app'
    ]
];

Usage

Application

When creating application, inject all settings:

$app = new \Slim\App(['settings' => $settings]);

Middleware

Add middleware with session settings:

$app->add(new \Adbar\SessionMiddleware($settings['session']));

Sessions

This package comes with session helper class, but if you wish to use only PHP session superglobal, then you can skip the rest of this guide and just enjoy coding! Session security configuration is done within middleware, so by using native superglobal your session is still secure (and encrypted if you set up encryption key in settings).

Session helper class uses namespaces for sessions, so basically session variables are always inside an array and array's key is the namespace. If you insert key 'user' with value 'John' into namespace 'users', superglobal $_SESSION looks like this:

Array
(
    [users] => Array
        (
            [user] => John
        )
)

If you don't need session namespaces in your application, then you can just ignore all namespace related parts on this guide and set namespace name in settings to null (it will default to namespace "slim_app").

Basic usage

You can inject session helper class in application container:

$container['session'] = function ($container) {
    return new \Adbar\Session(
        $container->get('settings')['session']['namespace']
    );
};

If session helper class is injected in application container and can be used as an object, i.e. like this:

$app->get('/', function (Request $request, Response $response) {
    // Namespace is now picked up from settings
    $this->session->set('user', 'John');
})->setName('home);

Or anywhere in your code:

$session = new \Adbar\Session('my_namespace');
// Namespace is now 'my_namespace'
$session->set('user', 'John');

If you don't use session class from container and don't inject namespace when creating session object, then namespace is always 'slim_app':

$session = new \Adbar\Session;
// Namespace is now 'slim_app'
$session->set('user', 'John');

Setting values

Set single value:

$session->set('user', 'John');

// Array style
$session['user'] = 'John';

// Magic method
$session->user = 'John';

// Set value to specific namespace
$session->setTo('my_namespace', 'user', 'John');

// Dot notation can be used with all methods except magic method:
$session->set('user.firstname', 'John');
$session['user.firstname'] = 'John';
$session->setTo('my_namespace', 'user.firstname', 'John');

Set multiple values at once:

$user = ['firstname' => 'John', 'lastname' => 'Smith'];
$session->set($user);

// Or just simply
$session->set([
    'firstname' => 'John',
    'lastname'  => 'Smith
]);

// Set values to specific namespace
$session->setTo('my_namespace', $user);

// Dot notation
$session->set([
    'user.firstname' => 'John',
    'user.lastname'  => 'Smith'
]);

Get value

echo $session->get('user');

// Get default value if key doesn't exist
echo $session->get('user', 'some default user');

// Array style
echo $session['user'];

// Magic method
echo $session->user;

// From specific namespace
echo $session->getFrom('my_namespace', 'user');

// Dot notation
echo $session->get('user.firstname');

Add value

$session->add('users', 'Mary');

// Dot notation
$session->add('home.kids', 'Jerry');

Multiple value at once:

$session->add([
    'users' => 'Sue',
    'cars'  => 'Toyota'
]);

// Dot notation
$session->add([
    'users'     => ['Katie', 'Ben'],
    'home.kids' => ['Carl', 'Tom']
]);

Check if value exists

if ($session->has('user')) {
    // Do something...
}

// Array style
if (isset($session['user'])) {
    // Do something...
}

// Magic method
if (isset($session->user)) {
    // Do something...
}

// In specific namespace
if ($session->hasIn('my_namespace', 'user')) {
    // Do something...
}

// Dot notation
if ($session->has('user.firstname')) {
    // Do something...
}

Delete value

$session->delete('user');

// Array style
unset($session['user']);

// Magic method
unset($session->user);

// From specific namespace
$session->deleteFrom('my_namespace', 'user');

// Dot notation
$session->delete('user.firstname');

Multiple values at once:

$session->delete(['user', 'home']);

// From specific namespace
$session->deleteFrom('my_namespace', ['user', 'home'']);

// Dot notation
$session->delete(['user', 'home.kids']);

Clear values

Clear all values:

$session->clear();

// From specific namespace
$session->clearFrom('my_namespace');

Clear all values within specific session key:

$session->clear('user');

// From specific namespace
$session->clearFrom('my_namespace', 'user');

// Dot notation
$session->clear('home.kids');

Multiple values at once:

$session->clear(['user', 'home']);

// From specific namespace
$session->clearFrom('my_namespace', ['user', 'home']);

// Dot notation
$session->clear(['user', 'home.kids']);

Destroy session completely

$session->destroy();

// Static method
\Adbar\Session::destroy();

Regenerate session id

$session->regenerateId();

// Static method
\Adbar\Session::regenerateId();

Change namespace

$session->setNamespace('another_namespace');

Get current namespace

$namespace = $session->getNamespace();

Delete namespace

$session->deleteNamespace('namespace_to_delete');

adbario/slim-secure-session-middleware 适用场景与选型建议

adbario/slim-secure-session-middleware 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 33.1k 次下载、GitHub Stars 达 29, 最近一次更新时间为 2016 年 09 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「middleware」 「session」 「slim」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 adbario/slim-secure-session-middleware 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 adbario/slim-secure-session-middleware 我们能提供哪些服务?
定制开发 / 二次开发

基于 adbario/slim-secure-session-middleware 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 29
  • Watchers: 4
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-09-11