ThinkPHP6 user login and registration: implementing user authentication function
Introduction:
User login and registration is one of the common requirements of most web applications. In ThinkPHP6, user login and registration operations can be easily realized by using the built-in user authentication function. This article will introduce how to implement user authentication function in ThinkPHP6, and attach code examples.
1. Introduction to user authentication function
User authentication refers to the process of verifying user identity. In web applications, user authentication usually includes user login and user registration.
User registration: Allow users to create a new account and save its related information to the database, such as user name, password, email, etc.
User login: The user uses a registered account to log in to the system, verify the legitimacy of the account, and access the resources required by the system.
2. Create a user model
First, we need to create a user model to operate user-related data.
Use the following command on the command line to generate a user model:
php think make:model User
The generated user model file is located in User.php in the appmodel directory.
In the User model, we need to define user-related fields and operations, such as user name, password, etc., as well as user registration and user login methods.
Code example:
namespace appmodel;
use thinkModel;
class User extends Model
{
// 定义用户字段 protected $schema = [ 'id' => 'int', 'username' => 'string', 'password' => 'string', 'email' => 'string', // 其他字段... ]; // 用户注册 public static function register($data) { // 验证数据合法性 // 保存用户数据到数据库 // 其他操作... } // 用户登录 public static function login($username, $password) { // 验证用户名和密码 // 登录操作... }
}
3. Create a user controller
Next, we need to create a user controller to handle user registration and login requests.
Use the following command on the command line to generate a user controller:
php think make:controller User
The generated user controller file is located in User.php in the appcontroller directory.
In the User controller, we need to define the user registration and user login methods, and call the corresponding methods in the user model for processing.
Code example:
namespace appcontroller;
use appmodelUser;
use thinkRequest;
use thinkController;
class User extends Controller
{
// 用户注册页面 public function register() { return view(); } // 用户注册 public function doRegister(Request $request) { // 获取用户提交的注册信息 $data = $request->post(); // 调用用户模型中的注册方法 User::register($data); } // 用户登录页面 public function login() { return view(); } // 用户登录 public function doLogin(Request $request) { // 获取用户提交的登录信息 $data = $request->post(); // 调用用户模型中的登录方法 User::login($data['username'], $data['password']); }
}
4. Create a user view interface
Finally, we need to create a view interface for user registration and login, which is used to display the user interface and receive data entered by the user. .
Create the user directory in the app iew directory, and create two files, register.html and login.html, in the user directory.
Code example (register.html):
<meta charset="UTF-8"> <title>用户注册</title>
head>
<form action="/user/doRegister" method="post"> <input type="text" name="username" placeholder="请输入用户名"><br> <input type="password" name="password" placeholder="请输入密码"><br> <input type="email" name="email" placeholder="请输入邮箱"><br> <input type="submit" value="注册"> </form>
Code example (login.html):
< ;!DOCTYPE html>
<meta charset="UTF-8"> <title>用户登录</title>
<form action="/user/doLogin" method="post"> <input type="text" name="username" placeholder="请输入用户名"><br> <input type="password" name="password" placeholder="请输入密码"><br> <input type="submit" value="登录"> </form>
The above is the detailed content of ThinkPHP6 user login and registration: realizing user authentication function. For more information, please follow other related articles on the PHP Chinese website!