PHP code example of k-ko / slim3-session-middleware
1. Go to this page and download the library: Download k-ko/slim3-session-middleware 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/ */
k-ko / slim3-session-middleware example snippets
$app = new \Slim\App();
$app->get('/', function (Request $request, Response $response) use ($container) {
if (!isset($container['session']['loggedIn'])) {
// ...
}
// ...
});
// Add as LAST middleware to run 1st
$app->add(new \Middleware\SessionMiddleware([
'autorefresh' => true,
'name' => 'myapp_session',
'lifetime' => '1 hour',
// optional:
'handler' => new \Session\MySQLiSessionHandler($container['mysqli'])
]));
$app->run();
$container = $app->getContainer();
$container['session'] = function ($container) {
return new \Session\Helper();
};
$app->get('/', function (Request $request, Response $response) use ($container) {
// Check if variable exists
$exists = $container['session']->has('my_key');
$exists = isset($container['session']->my_key);
$exists = isset($container['session']['my_key']);
// Get variable value
$value = $container['session']->get('my_key', 'default');
$value = $container['session']->my_key;
$value = $container['session']['my_key'];
// Take variable value out from session
$value = $container['session']->take('my_key', 'default');
// Set variable value
$container['session']->set('my_key', 'my_value');
$container['session']->my_key = 'my_value';
$container['session']['my_key'] = 'my_value';
// Remove variable
$container['session']->remove('my_key');
unset($container['session']->my_key);
unset($container['session']['my_key']);
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.