PHP code example of acfo / session

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

    

acfo / session example snippets


$container[\Acfo\Session\Session::class] = function () {
    return new \Acfo\Session\SessionImpl();
};

$container[\Acfo\Session\Middleware\Slim3\SessionMiddleware] = function ($container) {
    return new \Acfo\Session\Middleware\Slim3\SessionMiddleware(
        $container->get(\Acfo\Session\Session::class)
    );
};

$app->add(\Acfo\Session\Middleware\Slim3\SessionMiddleware::class);

$session = $container->get(\Acfo\Session\Session::class);

$value = $session->get('key'); // lazy load: first access will invoke start_session

$session->set('key', 1234);

$session->regenerate();

$session->delete('key');

$session->deleteAll();

$session->close(); // call close if you want to end a read/write session as soon as possible. 
 

class GetRequestReadOnlySessionStrategy implements ReadOnlySessionStrategy
{
    public function isReadOnly(ServerRequestInterface $request): bool
    {
        return $request->getMethod() == 'GET';
    }
}

$container[\Acfo\Session\Middleware\Slim3\SessionMiddleware] = function ($container) {
    $readOnlySessionStrategies = [
        new GetRequestReadOnlySessionStrategy()
    ];

    return new \Acfo\Session\Middleware\Slim3\SessionMiddleware(
        $container->get(\Acfo\Session\Session::class),
        $readOnlySessionStrategies
    );
};

$app->add(\Acfo\Session\Middleware\Slim3\SessionMiddleware::class);

$settings = \Acfo\Session\Middleware\Slim3\SessionMiddleware::RECOMMENDED_SETTINGS;

$container[\Acfo\Session\Middleware\Slim3\SessionMiddleware] = function ($container) {
    $settings = \Acfo\Session\Middleware\Slim3\SessionMiddleware::RECOMMENDED_SETTINGS;

    return new \Acfo\Session\Middleware\Slim3\SessionMiddleware(
        $container->get(\Acfo\Session\Session::class),
        null,
        $settings
    );
};


$app->add(\Acfo\Session\Middleware\Slim3\SessionMiddleware::class);