1. Go to this page and download the library: Download sulaco-tech/psr7-sessions 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/ */
sulaco-tech / psr7-sessions example snippets
use \Slim\Factory\AppFactory;
use \Psr\Http\Message\ResponseInterface as Response;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \SulacoTech\PSR7Sessions\SessionMiddleware;
use \SulacoTech\PSR7Sessions\SessionFileStorage;
use \SulacoTech\PSR7Sessions\SessionFileStorageConfiguration;
// create application
$app = AppFactory::create();
// prepare configuration
$sessionsDirectory = __DIR__ . '/../tmp/sessions';
$sessionName = 'example';
$sessionsExpirationTime = 300; // in seconds
$config = new SessionFileStorageConfiguration($sessionsDirectory, $sessionName, $sessionsExpirationTime);
// create storage with some configuration
$sessionStorage = new SessionFileStorage($config);
// call garbage collector
$sessionStorage->gc();
// create and add middleware
$app->add(new SessionMiddleware($sessionStorage));
// basic example
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
// get session
$session = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE);
// read and update session's data
$counter = $session->get('counter', 0);
$session->set('counter', ++ $counter);
// same instructions using array access style
//$counter = $session['counter'] ?? 0;
//$session['counter'] = ++ $counter;
// make a response
$response->getBody()->write("Hello, {$args['name']}! This page is visited $counter times.");
return $response;
});
// run application
$app->run();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.