PHP code example of italix / session

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

    

italix / session example snippets


use Italix\Session\Session;
use Italix\Session\Stores\PdoSessionStore;

use function Italix\Session\{cookie_spec, session_policy};

// Once, in configuration.
$store  = new PdoSessionStore($dm->get_connection());
$cookie = cookie_spec('__Host-session')->secure()->http_only()->same_site('Lax')->path('/');
$policy = session_policy()->idle_ttl(7200)->absolute_ttl(28800);

// Once per request, in one middleware.
Session::begin($store, $cookie, $request->getCookieParams()['__Host-session'] ?? null, $policy);

try {
    $response = $handler->handle($request);
} finally {
    $outcome = Session::end();
}

return $outcome->has_cookie()
    ? $response->withAddedHeader('Set-Cookie', $outcome->cookie_header())
    : $response;

Session::current()->get('locale_c');
Session::current()->set('locale_c', 'it');
Session::authenticate('account:12');   // rotates the identifier, keeps the data
Session::forget();                     // sign out

$sessions = new SessionRegister($store, null, $policy);   // or Session::register()

$sessions->of_subject('account:12');                       // device list
$sessions->revoke_subject('account:12', 'suspended');      // sign out everywhere
$sessions->revoke_others('account:12', Session::token());   // everywhere but here
$sessions->revoke($id_c);                                  // one device

$sessions->of_tenant('tenant:7');                  // who is working in this account right now
$sessions->revoke_tenant('tenant:7', 'unpaid');    // suspend the subscription — everybody, now

Session::set_tenant('tenant:7');   // subject_c stays 'staff:1'
Session::regenerate();             // the privileges changed, so the identifier does

if (!$sessions->can_find_by_subject()) {
    return $this->show('account/devices_unavailable');
}

Session::state();   // active | absent | expired_idle | expired_absolute | revoked | rotated

case 'rotated':
    // Killing the session that presented it is not enough: the thief may be
    // holding the current one.
    $sessions->revoke_subject((string) Session::refused_subject(), 'replay_detected');
    break;

if (!Session::authenticated_within(900)) {
    return $this->redirect_to('/step-up');
}

// after the password or the OTP is verified again:
Session::reauthenticate();

// set() is right for a scalar the caller owns.
Session::current()->set('locale_c', 'it');

// merge() is right for a structure two requests can touch. The closure is
// replayed against the stored value at write time, inside the store's lock.
Session::current()->merge('cart', static function (array $cart) use ($sku_c): array {
    $cart[$sku_c] = ($cart[$sku_c] ?? 0) + 1;

    return $cart;
});

session_policy()->idle_ttl(7200)->max_concurrent(2);

if (count($sessions->of_subject($subject_c)) >= $limit_n) { … }

Session::put(new AuthContext('staff', 12));
Session::typed(AuthContext::class);     // ?SessionPayload, or null

cookie_spec('__Host-session')->secure()->http_only()->same_site('Strict')->path('/');

Session::begin_native($cookie, $policy);

$sessions = new SessionRegister($store, null, $policy);

$removed_n = $sessions->gc();   // run it from cron, a queue, a scheduler — this library takes no view
bash
php src/Libs/Italix/Session/tests/SessionTest.php
php src/Libs/Italix/Session/tests/StoreTest.php