PHP code example of amphp / aerys-session

1. Go to this page and download the library: Download amphp/aerys-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/ */

    

amphp / aerys-session example snippets


$session->get('key'); // will read data stored in key 'key'

$session->lock();
$session->set('key', $data);
$session->commit(); // commits & unlocks

// regenerate the client id
$session->regenerate();

// force read from storage
$session->read();

// rollback what is `set()` in the session but has not been commit()ed yet
$session->rollback();

// destroy the session
$session->destroy();

use Amp\Http\Server\Request;
use Amp\Http\Server\RequestHandler;
use Amp\Http\Server\Response;
use Amp\Http\Server\Session\Session;

class SomeRequestHandler implements RequestHandler
{
    public function handleRequest(Request $request): Response
    {
        /** @var Session $session */
        $session = $request->getAttribute(Session::class);

        // any operations on the session

        // return the response
    }
}

$cookieAttributes = CookieAttributes::default()
    ->withDomain('amphp.org')
    ->withExpiry(new \DateTime('+30 min'))
    ->withSecure();

/** @var \Amp\Http\Server\Session\SessionFactory $factory */
$session = $factory->create($clientId);