Home  >  Article  >  Backend Development  >  Simply implement the function of logging in with an account through ThinkPHP

Simply implement the function of logging in with an account through ThinkPHP

不言
不言Original
2018-06-11 10:29:221372browse

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

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

HTML and CSS to implement dynamic background login page

How to use thinkphp to log in with mobile phone number and user name at the same time

About Thinkphp’s function code for SMS verification registration

The above is the detailed content of Simply implement the function of logging in with an account through ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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