Home>Article>Backend Development> ThinkPHP implements simple login function
This article mainly introduces the simple login function of ThinkPHP to everyone in detail, which has certain reference value. Interested friends can refer to it
Idea: Enter the account password at the front desk and customize one in the backend The function checkNamePwd() is used to verify whether the account password is correct. It is called in the controller. The checkNamePwd() method verifies the correctness of the account password by first searching for the password through the account, and then comparing the found password with the password entered by the user. If they are the same, the login is successful, otherwise the login fails!
First define a function checkNamePwd() in the model class
public function checkNamePwd($name,$pwd) { //①先根据$name查询是否存在指定名字的记录 //通过$name查找整条记录 $res = $this->where("mg_name='$name'")->find(); if ($res) { //②把查询到的记录的密码与用户输入的密码比较 if ($res['mg_pwd']===$pwd) { return $res; } }else{ return null; } }
The controller receives the information entered by the user and calls the checkNamePwd() method
$manager = new \Model\ManagerModel(); $name = $_POST['admin_user']; $pwd = $_POST['admin_psd']; //验证成功返回整条记录,否则返回null $info = $manager->checkNamePwd($name,$pwd); if ($info) { //验证成功,给用户信息session持久化操作(name,id) session('admin_id',$info['mg_id']); session('admin_name',$info['mg_name']); //跳转后台首页 $this->redirect('Index/index'); }else{ echo "用户名或密码错误"; }
Related recommendations:
thinkphp project how to customize the WeChat sharing description content
The above is the detailed content of ThinkPHP implements simple login function. For more information, please follow other related articles on the PHP Chinese website!