Home  >  Article  >  Backend Development  >  How to implement simple login function in ThinkPHP

How to implement simple login function in ThinkPHP

墨辰丷
墨辰丷Original
2018-05-23 14:05:391168browse

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.


Related recommendations:

How to implement automatic jump to website pagesPHP, JSP (Part 2)_javascript skills

The difference between quick sorting php and javascript_javascript skills

##File, FileReader and Ajax file upload example analysis ( php)_javascript skills

The above is the detailed content of How to implement simple login function in 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