PHP sessions compatibility with Swoole web-server

1.0.1 2018-09-30 04:45 UTC


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


  • 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


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

composer require upscale/swoole-session


Wrap your Swoole request handler into the session middleware:

require 'vendor/autoload.php';

use Upscale\Swoole\Session\SessionMiddleware;

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

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



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.


Licensed under the Apache License, Version 2.0.