PHP code example of itxiao6 / session

1. Go to this page and download the library: Download itxiao6/session library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

itxiao6 / session example snippets


use \Itxiao6\Session\SessionManager;
$session = \Itxiao6\Session\SessionManager::getSessionInterface();

$session -> set_deiver(new \Doctrine\Common\Cache\FilesystemCache(__DIR__.DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR));

$redis = new \Redis();
$redis->connect('127.0.0.1', 6319);
$cacheDriver = new \Doctrine\Common\Cache\RedisCache();
$cacheDriver->setRedis($redis);

$session -> set_deiver($cacheDriver);

$memcache = new \Memcache();
$memcache->connect('127.0.0.1', 11211);

$cacheDriver = new \Doctrine\Common\Cache\MemcacheCache();
$cacheDriver->setMemcache($memcache);
$session -> set_deiver($cacheDriver);

$memcached = new \Memcached();
$memcached->addServer($cacheConfig['Mamcached']['host'], $cacheConfig['Mamcached']['port']);

$cacheDriver = new \Doctrine\Common\Cache\MemcachedCache();
$cacheDriver->setMemcached($memcached);
$session -> set_deiver($cacheDriver);

$session -> set_deiver(new \Doctrine\Common\Cache\XcacheCache());

$session -> set_config(new \Itxiao6\Session\Tools\Config([
    'session_name'=>'PHPSESSION',
    'session_path'=>'/',
    'session_id_length'=>32,
    'session_id_type'=>1,
    'session_storage_prefix'=>'itxiao6_session_',
    // 默认有效期一天
    'session_expire'=>3600*24,
]));

try{
    // 启动会话
    $session -> start();
}catch (\Throwable $exception){
    // 打印错误
    var_dump($exception);
}

$session -> session() -> set('name','戒尺');

echo $session -> session() -> get('name');

// 创建http server
$http = new \swoole_http_server('0.0.0.0', 9501, SWOOLE_BASE);
// 监听request 事件
$http->on('request', function(\swoole_http_request $request, \swoole_http_response $response){
    /**
     * 获取Swoole 会话
     */
    $session = \Itxiao6\Session\SessionManager::getSwooleSessionInterface($request,$response);

    /**
     * 设置驱动(文件驱动)
     */
    $session -> set_deiver(new \Doctrine\Common\Cache\FilesystemCache(__DIR__.DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR));
    /**
     * 设置配置实例
     */
    $session -> set_config(new \Itxiao6\Session\Tools\Config());
    /**
     * 启动会话
     */
    try{
        $session -> start();
    }catch (\Throwable $exception){
        var_dump($exception);
    }
    /**
     * 设置一个值 到session 里
     */
    $session -> session() -> set('user_info',['nickname'=>'戒尺','phone'=>'15538147923','sub'=>['id'=>1]]) -> save();
    /**
     * 获取session 里的一个值
     */
    $response -> write(json_encode($session -> session() -> get('user_info')));
    /**
     * 结束请求
     */
    $response -> end();
});
/**
 * 启动http server
 */
$http -> start();