承接 monolyth/cesession 相关项目开发

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

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

monolyth/cesession

Composer 安装命令:

composer require monolyth/cesession

包简介

Alternative PHP session handler for Monolyth unframework

README 文档

README

Alternative PHP session handler for Monolyth unframework

Who is this for?

While PHP supports session handling, the implementation is less than stellar. Cesession provides an alternative with built-in helpers to use either an SQL database, memcached or a NoSQL database.

Cesession offers a seamless interface with the well-known $_SESSION superglobal, so any existing code should Just Work(tm).

An added advantage of using SQL based databases is that you can set up a foreign key constraint between the current user and the current session, so people get automatically logged out.

Installation

Composer (recommended)

$ composer require monolyth/cesession

Manual

  1. Clone or download the repository;
  2. Add the Monolyth\Cesession namespace to your autoloader for /path/to/cesession/src.
  3. Create the relevant table (see scripts in ./info/sql)

That's it!

Setting up

To begin, before any call to session_start create the Monolyth\Cesession\Session object and register a handler. Currently Cesession ships with a Pdo handler that does exactly what its name implies (store the session data in a PDO-compatible database, e.g. PostgreSQL or MySQL):

<?php

use Monolyth\Cesession\Session;
use Monolyth\Cesession\Handler;

$session = new Session('my-session-name');
$db = new PDO('dsn', 'user', 'pass');
$handler = new Handler\Pdo($db);
$session->registerHandler($handler);
session_start();

Alternatively, there is also a Memcached handler. Since objects stored in Memcached can be deleted at any time, this should only be used as a fallback in conjunction with a more persistent handler like Pdo.

Database tables

Example schemas are included in the ./info directory. These contain the minimum columns needed for the Pdo handler to work; depending on your needs you can add extra columns (e.g. auth for the currently logged in user's id).

Registering handlers

To register a handler, simply call the registerHandler method on the $session object and pass in a handler object. Each handler object must implement the Monolyth\Cesession\Handler interface. This is done both for type hinting and to ensure the required methods exist. The Handler interface is a subset of PHP's built-in SessionHandlerInterface.

The optional second argument to registerHandler is a probability percentage between 0 and 100. This signifies the probability that for supporting calls, the action is also delegated to the next handler (if defined).

E.g., say you want to store sessions in Memcached (fast!) but persist to a PDO backend every ten calls on average:

<?php

$session->registerHandler(new Handler\Memcached($memcached), 10);
$session->registerHandler(new Handler\Pdo($db));

Note: currently only the Pdo and Memcached handlers are supported out of the box.

Forcing an operation on all handlers

Sometimes you'll want to ensure an operation gets persisted to all handlers, for instance when a user's authentication state changes. Use the force method for this. The first argument is the session method you need to call, the second an array of arguments to pass:

<?php

// Force emptying of the current session on all handlers:
$session->force(
    'write',
    [session_id(), ['data' => serialize([])] + $session::$session]
);

Internally this calls the method on all defined handlers with a probability of 100%. Note that using force only makes sense if you have multiple handlers defined with varying probabilities.

The forwarding is done directly on the handlers, hence the arguments passed are slightly different than on the main Session object. Most importantly, $data is not passed to write as a string but as a hash with augmented meta information about the session.

Writing your own handlers

See the examples in ./src/Handler. It's simple enough.

Session encoding/decoding

By default, Cesession uses the session.serialize_handler PHP ini setting to en/decode session data. You can override this, e.g. in your php.ini or by calling ini_set('session.serialize_handler, 'new value'). See the PHP manual for valid values, but say you wanted to store session data using regular serialize and unserialize calls, you would do this:

ini_set('session.serialize_handler', 'php_serialize');

This allows you to modify the session data more easily from other places in your code (say, a cronjob) without having to resort to weird trickery.

monolyth/cesession 适用场景与选型建议

monolyth/cesession 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 376 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 07 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 monolyth/cesession 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 376
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 17
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-07-10