
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',
// 其他字段...
];
// 用戶注冊(cè)
public static function register($data)
{
// 驗(yàn)證數(shù)據(jù)合法性
// 保存用戶數(shù)據(jù)到數(shù)據(jù)庫(kù)
// 其他操作...
}
// 用戶登錄
public static function login($username, $password)
{
// 驗(yàn)證用戶名和密碼
// 登錄操作...
}
}
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
{
// 用戶注冊(cè)頁(yè)面
public function register()
{
return view();
}
// 用戶注冊(cè)
public function doRegister(Request $request)
{
// 獲取用戶提交的注冊(cè)信息
$data = $request->post();
// 調(diào)用用戶模型中的注冊(cè)方法
User::register($data);
}
// 用戶登錄頁(yè)面
public function login()
{
return view();
}
// 用戶登錄
public function doLogin(Request $request)
{
// 獲取用戶提交的登錄信息
$data = $request->post();
// 調(diào)用用戶模型中的登錄方法
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>用戶注冊(cè)</title>
head>
<form action="/user/doRegister" method="post">
<input type="text" name="username" placeholder="請(qǐng)輸入用戶名"><br>
<input type="password" name="password" placeholder="請(qǐng)輸入密碼"><br>
<input type="email" name="email" placeholder="請(qǐng)輸入郵箱"><br>
<input type="submit" value="注冊(cè)">
</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="請(qǐng)輸入用戶名"><br>
<input type="password" name="password" placeholder="請(qǐng)輸入密碼"><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!