定制 michel/session 二次开发

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

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

michel/session

Composer 安装命令:

composer require michel/session

包简介

PHP Session is a PHP library that optimizes session management, utilizing PHP's native session handling mechanisms for enhanced security and efficiency.

README 文档

README

Introduction

The SessionStorage is a PHP library that provides a simple implementation of the SessionStorageInterface. It allows developers to work with PHP sessions in a convenient way, providing methods to get, set, check, and remove session data. This library requires PHP version 7.4 or higher.

Installation

Use Composer

Composer Require

composer require michel/session

Usage

To start using the SessionStorage, you need to create an instance of the NativeSessionStorage class. Here's an example of how to do it:

use Michel\Session\Storage\NativeSessionStorage;

// Create a new session storage instance

/**
 * Constructor for NativeSessionStorage class.
 *
 * @param array $options Options for session start.
 *                      Possible options:
 *                      - 'name': Session name
 *                      - 'lifetime': Session lifetime
 *                      - 'path': Session save path
 *                      - 'domain': Session domain
 *                      - 'secure': Set to true for secure session
 *                      - 'httponly': Set to true to only allow HTTP access
 * @throws \RuntimeException If session start fails.
 */
$sessionStorage = new NativeSessionStorage();

Use Cases

The SessionStorage library offers the following methods to interact with PHP sessions:

1. Check if a key exists in the session:

if ($sessionStorage->has('user_id')) {
    // Do something with the user_id
}

2. Get the value of a session key:

$userName = $sessionStorage->get('username', 'Guest');
// If the 'username' key exists in the session, $userName will be set to its value,
// otherwise, it will be set to 'Guest'.

3. Set a value in the session:

$userId = 123;
$sessionStorage->put('user_id', $userId);

4. Remove a key from the session:

$sessionStorage->remove('user_id');

5. Get all session data as an array:

$allData = $sessionStorage->all();

Interface Implementation

The NativeSessionStorage class implements the SessionStorageInterface, which extends the ArrayAccess interface. As a result, any class implementing the SessionStorageInterface should also implement the methods defined in the ArrayAccess interface. Here's how you can implement the SessionStorageInterface in a custom class:

use Michel\Session\Storage\SessionStorageInterface;

class MyCustomSessionStorage implements SessionStorageInterface
{
    private array $storage;

    public function __construct()
    {
        // Initialize your custom storage mechanism here
        // For example, you could use a database, Redis, or any other storage solution.
        // In this example, we will use an array as a simple custom storage mechanism.
        $this->storage = [];
    }

    public function get(string $key, $default = null)
    {
        return $this->storage[$key] ?? $default;
    }

    public function put(string $key, $value = null): void
    {
        $this->storage[$key] = $value;
    }

    public function all(): array
    {
        return $this->storage;
    }

    public function has(string $key): bool
    {
        return isset($this->storage[$key]);
    }

    public function remove(string $key): void
    {
        unset($this->storage[$key]);
    }

    // Implementing ArrayAccess methods

    public function offsetExists($offset): bool
    {
        return isset($this->storage[$offset]);
    }

    public function offsetGet($offset)
    {
        return $this->get($offset);
    }

    public function offsetSet($offset, $value): void
    {
        $this->put($offset, $value);
    }

    public function offsetUnset($offset): void
    {
        $this->remove($offset);
    }
}

In this example, we create a custom session storage class MyCustomSessionStorage, which implements the SessionStorageInterface. It uses a simple array to store session data, but you can replace this with any custom storage mechanism like a database, Redis, etc., depending on your specific use case.

Conclusion

The SessionStorage library simplifies working with PHP sessions by providing a clean and easy-to-use interface. It is well-suited for applications that need to manage session data efficiently.

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests to help improve the library.

License

This library is open-source software licensed under the MIT license.

michel/session 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-15