pavel852/php-auth
Composer 安装命令:
composer require pavel852/php-auth
包简介
Simple authentification system for PHP.
README 文档
README
Overview
auth.php is a simple PHP authentication library that provides essential user authentication functionalities without relying on an internal database. It uses arrays to manage user data and MD5 for password hashing. The library includes features such as user registration, login, logout, password reset, and email verification.
Functions
auth_settings($params = "")
Sets the configuration parameters for the authentication library.
- Parameters:
$params(string): Configuration parameters in the format"key=value, key=value, ...".
- Usage:
auth_settings("email_verification=true, numeric_password_length=6, reset_token_expiry=3600, verification_token_expiry=86400, passwd_verification=true");
auth_version()
Returns the current version of the authentication system.
- Returns:
- (string) The version number.
- Usage:
print auth_version(); // Output: 1.0
generateNumericPassword($length = 6)
Generates a random numeric password.
- Parameters:
$length(int): The number of digits in the password.
- Returns:
- (string) The generated numeric password.
- Usage:
$password = generateNumericPassword(6); // e.g., "482915"
generateToken()
Generates a random token.
- Returns:
- (string) A 32-character hexadecimal token.
- Usage:
$token = generateToken(); // e.g., "a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6"
auth_register($username, $password = null, $userid = null, $email = null)
Registers a new user.
- Parameters:
$username(string|array): Username or an array of user data.$password(string|null): Password (optional if email verification is enabled).$userid(mixed): User ID (optional).$email(string|null): Email address (optional).
- Returns:
- (array|bool|string) User data array on success or
trueif no email verification is required, otherwise an error message.
- (array|bool|string) User data array on success or
- Usage:
$user = auth_register('john_doe', 'securepassword', 1001, 'john@example.com');
auth_login($identifier, $password, $user_data)
Logs in a user.
- Parameters:
$identifier(string|int): Username or User ID.$password(string): Password.$user_data(array): User data array.
- Returns:
- (bool|string)
trueon success, otherwise an error message.
- (bool|string)
- Usage:
$result = auth_login('john_doe', 'securepassword', $user_data); if ($result === true) { echo "Login successful!"; } else { echo "Login failed: " . $result; }
auth_logout()
Logs out the current user.
- Returns:
- (void)
- Usage:
auth_logout(); echo "You have been logged out.";
auth_request_password_reset($email, $user_data)
Requests a password reset.
- Parameters:
$email(string): User's email address.$user_data(array): User data array.
- Returns:
- (array|bool|string) Array with reset token and expiry on success, otherwise an error message.
- Usage:
$reset = auth_request_password_reset('john@example.com', $user_data); if (is_array($reset)) { echo "Reset token: " . $reset['reset_token']; } else { echo "Error: " . $reset; }
auth_reset_password($token, $new_password, &$user_data)
Resets the user's password using a reset token.
- Parameters:
$token(string): Reset token.$new_password(string): New password.&$user_data(array): Reference to user data array.
- Returns:
- (bool|string)
trueon success, otherwise an error message.
- (bool|string)
- Usage:
$result = auth_reset_password('reset_token_here', 'newpassword', $user_data); if ($result === true) { echo "Password reset successful!"; } else { echo "Error: " . $result; }
auth_verify_email($token, &$user_data)
Verifies the user's email using a verification token.
- Parameters:
$token(string): Verification token.&$user_data(array): Reference to user data array.
- Returns:
- (array|bool|string)
trueon success or array with additional data if password verification is required, otherwise an error message.
- (array|bool|string)
- Usage:
$result = auth_verify_email('verification_token_here', $user_data); if ($result === true) { echo "Email verified successfully!"; } elseif (is_array($result)) { echo "Set password using token: " . $result['set_password_token']; } else { echo "Error: " . $result; }
auth_get_user()
Gets the currently logged-in user.
- Returns:
- (string|null) Username or
nullif not logged in.
- (string|null) Username or
- Usage:
$user = auth_get_user(); echo "Logged in as: " . $user;
auth_is_logged_in()
Checks if a user is currently logged in.
- Returns:
- (bool)
trueif logged in, otherwisefalse.
- (bool)
- Usage:
if (auth_is_logged_in()) { echo "User is logged in."; } else { echo "User is not logged in."; }
Examples
1. Registration and Login with Username and Password
Registration
<?php require_once 'auth/init.php'; auth_settings("email_verification=false, passwd_verification=false"); $user = auth_register('john_doe', 'securepassword', 1001, 'john@example.com'); if ($user === true) { echo "Registration successful!"; } else { echo "Registration failed: " . $user; } ?>
pavel852/php-auth 适用场景与选型建议
pavel852/php-auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pavel852/php-auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pavel852/php-auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 17
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-11-01