承接 abollinger/session 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

abollinger/session

最新稳定版本:v1.2.1

Composer 安装命令:

composer require abollinger/session

包简介

PHP Session management for the partez framework

README 文档

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; otherwise false.

login(array $arr): void

Description: Logs in a user by setting session variables and saving the token to the database.

  • Parameters:
    • $arr (array): Contains userId and token.

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): Contains userId.

Example Workflow

  1. Start a session:
$session = new Session();
  1. Log in a user:
$session->login([
    "userId" => "exampleUser",
    "token"  => "exampleToken"
]);
  1. Verify user authentication:
if ($session->isLoggedAndAuthorized()) {
    echo "User is authenticated!";
} else {
    echo "Authentication failed.";
}
  1. 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.

统计信息

  • 总下载量: 288
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-12