PHP code example of codeinc / session-manager

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

    

codeinc / session-manager example snippets



use CodeInc\SessionManager\SessionManager;

// the session manager need a session handler to start
$sessionManager = new SessionManager(
	new MySessionHandler() //  any handler implementing HandlerInterface
);
$sessionManager->setName("AGreatSession");
$sessionManager->setExpire(30); // minutes
$sessionManager->setValidateClientIp(true);
$session = $sessionManager->start($psr7ServerRequest); // the PSR-7 server request

// SessionManager implement ArrayAccess 
$session["test"] = "Hello wold!";
echo $session["test"];

// SessionManager is also iterable
foreach ($session as $var => $value) {
	echo "$var = $value\n";
}


use CodeInc\SessionManager\SessionManager;
use CodeInc\SessionManager\SessionMiddleware;

// the middleware needs the session manager
$sessionManager = new SessionManager(new MySessionHandler());

// instantiating the middleware and processing the PSR-7 request, producing a PSR-7 response
// the middleware will take car of starting the session and will attache the session
// data to the PSR-7 request attributes.
$middleware = new SessionMiddleware($sessionManager);
$psr7Response = $middleware->process(
	$psr7ServerRequest, 
	$somePsr15RequestHandler
);


use CodeInc\SessionManager\SessionMiddleware;

$session = SessionMiddleware::getSession($psr7ServerRequest);
$session["user_name"] = "John Smith";
echo $session["user_name"];