Home > Backend Development > PHP Tutorial > ThinkPHP implements simple login function

ThinkPHP implements simple login function

不言
Release: 2023-03-30 06:54:01
Original
1677 people have browsed it

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;
    }
  }
Copy after login

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 "用户名或密码错误";
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template