Coder Social home page Coder Social logo

monsterpipi / session Goto Github PK

View Code? Open in Web Editor NEW

This project forked from itxiao6/session

0.0 1.0 0.0 49 KB

基于Cookie 重写了PHP自带的 SESSION 机制,存储介质支持:本地模式、Mysql、Redis, php运行模式支持:LAMP、LNMP、SWOOLE

PHP 100.00%

session's Introduction

session

基于Cookie 重写了PHP自带的 SESSION 机制,存储介质支持:本地模式、Mysql、Redis,php运行模式支持:LAMP、LNMP、SWOOLE

1.引入入口

use \Itxiao6\Session\Session;

2.启动会话

1.本地存储方式(默认)

// 设置使用的存储介质
Session::driver(); // 默认为 Local
// 启动会话
Session::getInterface() -> start(__DIR__.'/SessionFile/');

2.MySql存储介质

$pdo = new \PDO("mysql:host=127.0.0.1;dbname=dbname",'username','passwd');
$session = Session::getInterface() -> driver('MySql') -> start($pdo,'session_table');

3.Redis存储介质

$redis = new \Redis();
$redis -> connect('127.0.0.1',6379);
# 启动会话
$session = Session::getInterface() -> driver('Redis') -> start($redis);

4.使用Session

# 设置值
$session -> set('name','戒尺');
# 获取值
var_dump($session -> get('name'));

更多存储机制可以无限扩展和替换

备注
1.存储器必须继承:Itxiao6\Session\Interfaces\Storage 接口
2.Session::interface('DriverName',Driver::class) -> driver('DriverName');的参数会传递到存储器的构造方法内,所有开发存储器只需要在构造方法获取连接 即可 
# 设置新的存储器 备注 Driver::class 必须继承 Itxiao6\Session\Interfaces\Storage
Session::getInterface() -> interface('Memcache',Driver::class);
$memcache = Memcache();
$memcache -> connect('127.0.0.1', 11211); 
use \Itxiao6\Session\Session;
// 设置存储介质 并启动会话
Session::getInterface() -> interface('Memcache',Driver::class) -> driver('Memcache') -> start($memcache);

附录1

SWOOLE 模式使用方式 操作和 驱动和上文使用方法一样,唯一的区别就是 调用 Session::getInterface()的时候需要传入 $request 和 $response

   // 创建http server
  $http = new swoole_http_server('0.0.0.0', 80, SWOOLE_BASE);
  // 监听request 事件
  $http->on('request', function(swoole_http_request $request, swoole_http_response $response){
    // 启动会话
    $session = Session::getInterface($request,$response) -> start(__DIR__.'/'.'SessionFile/');
    // 设置值
    $session -> set('name','戒尺');
    // 获取值 输出Hello World session 内的name值
    $res->write("hello world:".$session -> get('name'));
    // 结束请求
    $res->end();
  });

session's People

Contributors

itxiao6 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.