PHP code example of innmind / http-session

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

    

innmind / http-session example snippets


use Innmind\HttpSession\Manager\Native;
use Innmind\Http\{
    Response,
    Response\StatusCode,
    ServerRequest,
    Headers,
    Header\SetCookie,
    Header\SetCookie\Directive,
    Header\SetCookie\Domain,
};

$manager = Native::of();
$request = /* an instance of ServerRequest */

$session = $manager->start($request)->match(
    static fn($session) => $session,
    static fn() => throw new \RuntimeException('Unable to start the exception'),
);
// inject some data in the session
$manager->save($session);

$response = Response::of(
    StatusCode::ok,
    $request->protocolVersion(),
    Headers::of(
        SetCookie::of(
            $session->name()->toString(),
            $session->id()->toString(),
            Directive::httpOnly,
            Domain::of($request->url()->authority()->host()),
        ),
    ),
);
// send the response