PHP code example of wyrihaximus / react-http-middleware-session

1. Go to this page and download the library: Download wyrihaximus/react-http-middleware-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/ */

    

wyrihaximus / react-http-middleware-session example snippets


$server = new Server(
    $loop,
    /** Other Middleware */
    new SessionMiddleware(
        'CookieName',
        $cache, // Instance implementing React\Cache\CacheInterface
        [ // Optional array with cookie settings, order matters
            0, // expiresAt, int, default
            '', // path, string, default
            '', // domain, string, default
            false, // secure, bool, default
            false // httpOnly, bool, default
        ],
    ),
    /** Other Middleware */
    function (ServerRequestInterface $request) {
        $session = $request->getAttribute(SessionMiddleware::ATTRIBUTE_NAME);

        // Overwrite session contents, the details of changing specific keys is up to you
        $session->setContents([
            'foo' => 'bar',
        ]);

        // Get session contents
        var_export($session->getContents()); // Prints something like: ['foo' = 'bar']

        return new Response();
    }
);

$array = $session->toArray();
// Transfer to child process
$session = (new Session('', [], new RandomBytes()))->fromArray($array);
// The same can be done transferring changes back to the parent