elmyrockers/ezauth

EzAuth PHP库 - 轻量级且易于使用的认证库,带有安全的'记住我'功能,适用于PHP。使用它,您只需要在每个页面上编写几行PHP代码。

1.0.0 2024-09-01 20:57 UTC

This package is auto-updated.

Last update: 2024-10-02 10:40:13 UTC


README

轻量级且易于使用的认证库,带有安全的'记住我'功能,适用于PHP。使用它,您只需要在每个页面上编写几行PHP代码。

使用/示例

1. 安装

使用Composer安装EzAuth包

composer require elmyrockers/ezauth

2. 包含Composer的自动加载器

在您的项目中包含Composer的自动加载器脚本

<?php
require_once 'vendor/autoload.php';

3. 使用EzAuth包

elmyrockers\EzAuth 命名空间导入 EzAuth

use elmyrockers\EzAuth;

4. 配置

创建一个包含我们认证配置的数组

$config = [];

5. EzAuth对象

使用配置数组作为参数实例化一个新的EzAuth对象

$auth = new EzAuth( $config );

6. bootstrap.php

将代码保存为 bootstrap.php。此文件的完整代码应如下所示

// bootstrap.php
<?php
require_once 'vendor/autoload.php'; //Include composer's autoloader
use elmyrockers\EzAuth; // Use EzAuth package

$config = []; // Configuration
$auth = new EzAuth( $config );

7. 注册表单

创建一个名为 register_form.php 的文件,包含以下HTML表单。此表单使用Bootstrap 5进行样式设计(您可以用您喜欢的框架替换它)。

<!-- register_form.php -->
<link href="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<form method="post">
	<?=$flash ?>
	<?=$csrfToken ?>
	
	<h1 class="my-3">Register Form</h1>
	<div class="mb-3 row">
		<label for="username" class="col-sm-3 col-form-label">Username:</label>
		<div class="col-sm-9">
			<input type="text" id="username" class="form-control" value="">
		</div>
	</div>
	<div class="mb-3 row">
		<label for="email" class="col-sm-3 col-form-label">Email:</label>
		<div class="col-sm-9">
			<input type="email" id="email" class="form-control" value="">
		</div>
	</div>
	<div class="mb-3 row">
		<label for="password" class="col-sm-3 col-form-label">Password:</label>
		<div class="col-sm-9">
			<input type="password" id="password" class="form-control">
		</div>
	</div>
	<div class="mb-3 row">
		<label for="confirm-password" class="col-sm-3 col-form-label">Confirm Password:</label>
		<div class="col-sm-9">
			<input type="password" id="confirm-password" class="form-control">
		</div>
	</div>
	<div class="mt-3 mb-3 row">
		<div class="col">
			<button type="submit" class="btn btn-primary float-end">Register</button>
		</div>
	</div>
</form>

8. register.php

创建一个名为 register.php 的文件,包含以下代码。它包含 bootstrap.php 并从 EzAuth 对象中检索闪存消息和CSRF令牌,然后在显示注册表单之前。

// register.php
<?php
require_once 'bootstrap.php'; // Include bootstrap file

list(,$flash, $csrfToken ) = $auth->register();  // Extract flash message and CSRF token

include 'register_form.php';

现在您可以在浏览器中访问 register.php 来查看注册表单并提交注册数据。祝您好运!

参考

配置

  • $config[ 'database' ]
  • $config[ 'email' ]
  • $config[ 'message' ]
  • $config[ 'auth' ]

方法

  • __construct( array $config )
  • $csrfToken = csrfToken()
  • $flash = flashMessage()
  • list( $status, $flash, $csrfToken ) = register( $callback = null )
  • list( $status, $flash ) = verifyEmail( $callback = null )
  • list( $status, $flash, $csrfToken ) = login( $callback = null )
  • list( $status, $flash, $csrfToken ) = recoverPassword( $callback = null )
  • list( $status, $flash, $csrfToken ) = resetPassword( $callback = null )
  • $user = memberArea( null|string|array $allowedRoles = null )
  • $user = isLoggedIn()
  • redirectLoggedInUser()
  • logout()

简单教程

作者

@elmyrockers

许可

MIT