定制 mmdm/sim-session 二次开发

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

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

mmdm/sim-session

Composer 安装命令:

composer require mmdm/sim-session

包简介

A simple yet nice session management library

README 文档

README

A library for session management.

Features

  • Simple sessions
  • Time sessions
  • Flash sessions
  • Encrypted and secure
  • Easy to use

Install

composer

composer require mmdm/sim-session

Or you can simply download zip file from github and extract it, then put file to your project library and use it like other libraries.

Just add line below to autoload files:

require_once 'path_to_library/autoloader.php';

and you are good to go.

How to use

// to instance a session object (without crypt)
$session = new Session();
// set a session
$session->set($key, $value);
// get a session
$theValue = $session->get($key);

Note: When you instantiate new session object, it will start session if it did not started yet.

Use with Crypt library

If you need more security on sessions, use Crypt library.

// send crypt instance through session
// constructure (dependency injection)
$session = new Session($crypt);
// now your sessions are safe

If you don't need some of your sessions to be secure, pass false as last parameter of set methods.

Available functions

  • start(bool $regenerate = false, bool $delete_old_session = false)

This method starts session. To regenerate it, pass true as first argument.

NOTE: It checks if session is started yet, if not then start it.

// start PHP sessions
$session->start();
// regnerate session id
$session->start(true);

NOTE: By default it'll not delete previous session but you can make this happen by send boolean as second parameter.

  • close()

This method close a started session.

// close PHP session
$session->close();
  • hasStart(): bool

This method check if PHP session has started.

// close PHP session
$session->hasStart();
  • set(string $key, $value, bool $encrypt = true): ISession

This method set $key to session with $value. To prevent encrypting when Crypt is specified through construct method, pass false as last parameter to not encrypt $value.

Note: To store multidimensional array in session, $key can be dotted separated strings.

exp. foo.bar => $_SESSION['foo']['bar'].

// to set a session
$session->set('foo', 'I am a normal session');
// or
$session->set('foo.bar', 'I am a normal session');
  • get(string $key = null, $prefer = null): mixed

This method get value of $key from session. If there is no session with key of $key it will return $prefer instead that can be specified through second argument - default is NULL.

Note: To get multidimensional array from session, $key can be dotted separated strings.

exp. foo.bar => $_SESSION['foo']['bar'].

// to get a session
$session->get('foo');
// or
$session->get('foo.bar', 'not set');
  • remove(string $key): ISession

This method removes value of $key from session.

Note: To remove multidimensional array from session, $key can be dotted separated strings.

exp. foo.bar => $_SESSION['foo']['bar'].

// to remove a session
$session->remove('foo');
// or
$session->remove('foo.bar');
  • has(string $key): bool

This method checks if $key is exists in sessions.

Note: To check multidimensional array in session, $key can be dotted separated strings.

exp. foo.bar => $_SESSION['foo']['bar'].

// to check existence of a session
$session->has('foo');
// or
$session->has('foo.bar');
  • setTimed(string $key, $value, $time = 300, bool $encrypt = true): ISession

This method is like simple set method, the only difference is $time. With this feature you can define timer for a session value. Default value is 300 seconds.

// to set a timer session
$session->setTimed('foo', 'I will expire after 10 seconds', 10);
// or
$session->setTimed('foo.bar');
  • getTimed(string $key = null, $prefer = null): mixed

This method is like simple get method.

// to set a timer session
$session->getTimed('foo');
// or
$session->getTimed('foo.bar', 'not set');
  • removeTimed(?string $key): ISession

This method is like simple remove method. The only difference is you can pass NULL to remove all timer sessions

// to remove a timer session
$session->removeTimed('foo');
// or
$session->removeTimed('foo.bar');
  • hasTimed(string $key): bool

This method is like simple has method.

// to check existence of a timer session
$session->hasTimed('foo');
// or
$session->hasTimed('foo.bar');
  • setFlash(string $key, $value, bool $encrypt = true): ISession

This method is like simple set method.

// to set a flash session
$session->setFlash('foo');
// or
$session->setFlash('foo.bar');
  • getFlash(string $key = null, $prefer = null, $delete = true): mixed

This method is like simple get method. The only difference is after get a flash session, default behavior is to remove that session, but you can make it not to remove through last parameter.

// to get a flash session
$session->getFlash('foo');
// or
$session->getFlash('foo.bar', 'not set');
  • removeFlash(?string $key = null): ISession

This method is like simple remove method. The only difference is you can pass NULL to remove all timer sessions

// to remove a flash session
$session->removeFlash('foo');
// or
$session->removeFlash('foo.bar', 'not set');
  • hasFlash(string $key): bool

This method is like simple has method.

// to check existence of a flash session
$session->hasFlash('foo');
// or
$session->hasFlash('foo.bar');

Dependencies

There is just one dependency and it is Crypt library. With this feature, if any session hijacking happens, they can't see actual data because it is encrypted.

License

Under MIT license.

mmdm/sim-session 适用场景与选型建议

mmdm/sim-session 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 39 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 08 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-08-01