Description

Workerman has strengthened its support for HTTP services since version 4.x. It introduces request classes, response classes, session classes, and SSE. If you want to use Workerman's HTTP services, it is strongly recommended to use Workerman 4.x or higher.

Getting the session object

$session = $request->session();

Example

use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker('http://0.0.0.0:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $session = $request->session();
    $session->set('name', 'tome');
    $connection->send($session->get('name'));
};

// Run the worker
Worker::runAll();

Notes

  • The session must be operated on before the $connection->send() call.
  • The session will automatically save modifications when the object is destroyed, so do not store the object returned by $request->session() in a global array or class member which may prevent the session from saving.
  • The session is stored in disk files by default; for better performance, it is recommended to use Redis.

Getting all session data

$session = $request->session();
$all = $session->all();

It returns an array. If there is no session data, it will return an empty array.

Example

use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker('http://0.0.0.0:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $session = $request->session();
    $session->set('name', 'tom');
    $connection->send(var_export($session->all(), true));
};

// Run the worker
Worker::runAll();

Getting a specific value from the session

$session = $request->session();
$name = $session->get('name');

If the data does not exist, it returns null.

You can also pass a default value as the second argument to the get method, which will be returned if the corresponding value is not found in the session array. For example:

$session = $request->session();
$name = $session->get('name', 'tom');

Example

use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker('http://0.0.0.0:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $session = $request->session();
    $connection->send($session->get('name', 'tom'));
};

// Run the worker
Worker::runAll();

Storing session

Use the set method to store a specific piece of data.

$session = $request->session();
$session->set('name', 'tom');

The set method has no return value, and the session will be automatically saved when the session object is destroyed.

When storing multiple values, use the put method.

$session = $request->session();
$session->put(['name' => 'tom', 'age' => 12]);

Similarly, put also has no return value.

Example

use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker('http://0.0.0.0:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $session = $request->session();
    $session->set('name', 'tom');
    $connection->send($session->get('name'));
};

// Run the worker
Worker::runAll();

Deleting session data

Use the forget method to delete a specific item or multiple session data.

$session = $request->session();
// Delete one item
$session->forget('name');
// Delete multiple items
$session->forget(['name', 'age']);

Additionally, the system provides a delete method, which differs from the forget method in that delete can only remove one item.

$session = $request->session();
// Equivalent to $session->forget('name');
$session->delete('name');

Example

use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker('http://0.0.0.0:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $request->session()->forget('name');
    $connection->send('ok');
};

// Run the worker
Worker::runAll();

Getting and deleting a specific value from the session

$session = $request->session();
$name = $session->pull('name');

This is equivalent to the following code:

$session = $request->session();
$value = $session->get($name);
$session->delete($name);

If the corresponding session does not exist, it returns null.

Example

use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker('http://0.0.0.0:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $connection->send($request->session()->pull('name'));
};

// Run the worker
Worker::runAll();

Deleting all session data

$request->session()->flush();

There is no return value, and the session will be automatically deleted from storage when the session object is destroyed.

Example

use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker('http://0.0.0.0:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $request->session()->flush();
    $connection->send('ok');
};

// Run the worker
Worker::runAll();

Checking if corresponding session data exists

$session = $request->session();
$has = $session->has('name');

The above will return false if the corresponding session does not exist or if the corresponding session value is null; otherwise, it returns true.

$session = $request->session();
$has = $session->exists('name');

The above code is also used to check if session data exists, with the difference being that when the corresponding session item's value is null, it will still return true.

Notes

When using sessions, it is not recommended to directly store instances of classes, especially instances of classes from uncontrollable sources, as deserialization may pose potential risks.