PHP code example of session-interop / utils.arraysession

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

    

session-interop / utils.arraysession example snippets


namespace Usage;
use Interop\Session\SessionInterface;
class UserService {
      public function isConnected(SessionInterface $session) {
          if ($session->has("userId")) {
	     	   return true;
	        }
	       return false;
      }
      public function login(SessionInterface $session, $userId) {
        if ($this->isConnected($session)) {
          return false;
  	    }
  	    $session->set("userId", $userId);
  	    return true;
      }
      public function logoff(SessionInterface $session) {
        if ($this->isConnected($session)) {
	     	     $session->remove("userId");
		           return true;
	       }
	      return false;
      }
}

use Interop\Session\Utils\ArraySession\ArraySession;
use Usage\UserService;
= new ArraySession($_SESSION, "myprefix");

// Check if the user is connected

if ($userService->isConnected($session)) {
  // logoff the user
  $userService->logoff($session);
}
else {
  // login the user
  $userService->login($session, $userId);
}