upscale/swoole-session

PHP sessions compatibility with Swoole web-server

1.0.1 2018-09-30 04:45 UTC

README

This library implements compatibility of the native PHP sessions with the Swoole web-server.

Features:

  • Transparent session start/stop
  • Session ID in cookies or query string
  • Native or custom session ID generator
  • Automatic session data persistence
  • Compliance with PHP session configuration

Installation

The library is to be installed via Composer as a dependency:

composer require upscale/swoole-session

Usage

Wrap your Swoole request handler into the session middleware:

require 'vendor/autoload.php';

use Upscale\Swoole\Session\SessionMiddleware;

$server = new \Swoole\Http\Server('127.0.0.1', 8080);

$server->on('request', new SessionMiddleware(function ($request, $response) {
    $_SESSION['data'] = $_SESSION['data'] ?? rand();
    $response->end($_SESSION['data']);
}));

$server->start();

Caveats

Direct output bypassing Swoole response is prohibited. Writing to the standard output stream violates the headers_sent() requirement of the PHP session functions.

PHP sessions are synchronous and blocking by design as explained by the Swoole team. Asynchronous libraries, such as itxiao6/session or swoft-cloud/swoft-session, built specifically for Swoole are recommended instead.

License

Licensed under the Apache License, Version 2.0.