abollinger / session
PHP Session management for the partez framework
README
Abollinger\Session is a PHP library designed to manage user session-related functionalities. It offers an easy-to-use interface for session management, user authentication, login, and logout processes, leveraging PHP's native session handling mechanisms and SQLite for token storage.
Features
-
Session Management: Automatically initializes a session if not already active.
-
User Authentication: Supports authentication checks with token validation, including cross-server requests.
-
Login and Logout: Provides methods to securely log in and log out users, managing session variables and database records.
-
Error Handling: Logs errors for debugging and ensures secure exception handling.
Requirements
- PHP 7.4 or higher
- SQLite3 extension enabled
Installation
You can install the Abollinger\Session package using Composer. Run the following command:
composer require abollinger/session
Then, include the Composer autoloader in your project:
require_once 'vendor/autoload.php'; use Abollinger\Session;
Usage
Initialization
use Abollinger\Session; // Instantiate the session manager $session = new Session();
Login
$session->login([ "userId" => "exampleUser123", "token" => "secureToken123" ]);
Authentication Check
$isAuthenticated = $session->isLoggedAndAuthorized($isSameServer = true); if ($isAuthenticated) { echo "User is authenticated!"; } else { echo "Authentication failed."; }
Logout
$session->logout([ "userId" => "exampleUser123" ]);
API Reference
Constructor
__construct()
Initializes a session if none exists and sets up the SQLite connection.
Methods
isLoggedAndAuthorized(bool $isSameServer = false): bool
Description: Checks if a user is logged in and authorized.
-
Parameters:
-
$isSameServer (bool)
: Defaults to false. Determines whether to validate via session variables or headers. -
Returns:
true
if the user is authenticated; otherwisefalse
.
-
login(array $arr): void
Description: Logs in a user by setting session variables and saving the token to the database.
- Parameters:
$arr
(array): ContainsuserId
andtoken
.
logout(array $arr): void
Description: Logs out a user by removing session variables, deleting the token from the database, and destroying the session.
- Parameters:
$arr
(array): ContainsuserId
.
Example Workflow
- Start a session:
$session = new Session();
- Log in a user:
$session->login([ "userId" => "exampleUser", "token" => "exampleToken" ]);
- Verify user authentication:
if ($session->isLoggedAndAuthorized()) { echo "User is authenticated!"; } else { echo "Authentication failed."; }
- Log out the user:
$session->logout([ "userId" => "exampleUser" ]);
Licence
This library is licensed under the MIT License. For full license details, see the LICENCE
file distributed with this source code.
Author
Antoine Bollinger Email: abollinger@partez.net
For contributions, issues, or feedback, feel free to contact the author or open a GitHub issue.