定制 amphp/http-server-session 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

amphp/http-server-session

Composer 安装命令:

composer require amphp/http-server-session

包简介

An HTTP server plugin that simplifies session management for your applications. Effortlessly handle user sessions, securely managing data across requests.

README 文档

README

AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. This package provides an HTTP server plugin that simplifies session management for your applications. Effortlessly handle user sessions, securely managing data across requests.

Installation

This package can be installed as a Composer dependency.

composer require amphp/http-server-session

Usage

Basic usage

To read data from the session is straightforward:

$session->get('key'); // will read data stored in key 'key'

Note that get() will return null if the data in key are not found.

In order to write data, the session must be lock()ed first so that it cannot be written from anywhere else.

$session->lock();
$session->set('key', $data);
$session->commit(); // commits & unlocks

Calling commit() will store the data in the session storage and unlock the session.

Other important methods of the Session class are:

// regenerate the client id
$session->regenerate();

// force read from storage
$session->read();

// rollback what is `set()` in the session but has not been commit()ed yet
$session->rollback();

// destroy the session
$session->destroy();

Use the middleware to access Session in a RequestHandler

As this package is a plugin for amphp/http-server there is a middleware implementation available that injects the Session instance into the attributes of the Request. When the middleware is used the session is accessible from the attributes:

use Amp\Http\Server\Request;
use Amp\Http\Server\RequestHandler;
use Amp\Http\Server\Response;
use Amp\Http\Server\Session\Session;

class SomeRequestHandler implements RequestHandler
{
    public function handleRequest(Request $request): Response
    {
        /** @var Session $session */
        $session = $request->getAttribute(Session::class);

        // any operations on the session

        // return the response
    }
}

Note that if the attribute Session::class is not registered then getAttribute will throw a MissingAttributeError.

The middleware will handle setting and reading a session cookie in the request/response as well as releasing all locks on the session after the request has been processed.

If you haven't used middleware in amphp/http-server, follow the instructions on how to use middle ware with amphp/http-server.

A simple example is provided here examples/simple.php.

The SessionMiddleware can be further configured from the constructor regarding four different aspects:

  • SessionFactory
  • CookieAttributes
  • Cookie name (default: 'session')
  • Request attribute (default: Session::class)

The CookieAttributes is used to configure different cookie properties such as the expiry or the domain:

$cookieAttributes = CookieAttributes::default()
    ->withDomain('amphp.org')
    ->withExpiry(new \DateTime('+30 min'))
    ->withSecure();

Using the factory to create an instance of Session

Internally the session works with 3 dependencies:

An instance of the Session can be constructed easily using the provided SessionFactory

/** @var \Amp\Http\Server\Session\SessionFactory $factory */
$session = $factory->create($clientId);

This library comes with two storage implementations

  • LocalSessionStorage - Default
  • RedisSessionStorage - Storage in Redis

and one session ID generator

  • Base64UrlSessionIdGenerator

The constructor of the SessionFactory allows to configure the factory with other implementations, so that subsequent calls to create() will use the new injected implementations. This can be beneficial in the certain scenarios, including testing.

Contributing

Please read our rules for details on our code of conduct, and the process for submitting pull requests to us.

Security

If you discover any security related issues, please use the private security issue reporter instead of using the public issue tracker.

License

The MIT License (MIT). Please see LICENSE for more information.

amphp/http-server-session 适用场景与选型建议

amphp/http-server-session 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.68k 次下载、GitHub Stars 达 19, 最近一次更新时间为 2018 年 03 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 amphp/http-server-session 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 19
  • Watchers: 8
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-13