PHP code example of anders / slim-session
1. Go to this page and download the library: Download anders/slim-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/ */
anders / slim-session example snippets
$app = new \Slim\App;
$app->add(new \Slim\Middleware\Session([
'name' => 'dummy_session',
'autorefresh' => true,
'lifetime' => '1 hour'
]));
$container = $app->getContainer();
// Register globally to app
$container['session'] = function ($c) {
return new \SlimSession\Helper;
};
$app->get('/', function ($req, $res) {
// or $this->session if registered
$session = new \SlimSession\Helper;
// Check if variable exists
$exists = $session->exists('my_key');
$exists = isset($session->my_key);
$exists = isset($session['my_key']);
// Get variable value
$my_value = $session->get('my_key', 'default');
$my_value = $session->my_key;
$my_value = $session['my_key'];
// Set variable value
$app->session->set('my_key', 'my_value');
$session->my_key = 'my_value';
$session['my_key'] = 'my_value';
// Merge value recursively
$app->session->merge('my_key', ['first' => 'value']);
$session->merge('my_key', ['second' => ['a' => 'A']]);
$letter_a = $session['my_key']['second']['a']; // "A"
// Delete variable
$session->delete('my_key');
unset($session->my_key);
unset($session['my_key']);
// Destroy session
$session::destroy();
// Get session id
$id = $this->session::id();
return $res;
});