thinkphp5中的登录验证

一.登录

在之前的基础上增加

1.控制器类增加login.php控制器(index/login.php)

2.增加与控制器类名相同的视图文件夹,html文件的名字是控制器中的方法名

3.数据库增加password字段。

thinkphp5中的登录验证_第1张图片

 

 

 方法一

直接在控制器里new 一个模型,然后调用实例化的模型对象的方法,实现数据的验证。

 

login.php

namespace app\index\controller;
use app\common\model\Teacher;

use think\Controller;
use think\Request;

class Login extends Controller
{
    // 用户登录表单
    public function index()
    {
       return $this->fetch();
    }

    // 处理用户提交的登录数据
    public function login()
    {
        // 接收post信息
        $postData = Request::instance()->post();

        // 验证用户名是否存在
        $map = array('username'  => $postData['username']);
        $Teacher = Teacher::get($map);

        // $Teacher要么是一个对象,要么是null。
        if (!is_null($Teacher) && $Teacher->getData('password') === $postData['password']) {
            // 用户名密码正确,将teacherId存session,并跳转至教师管理界面
            session('teacherId', $Teacher->getData('id'));
            return $this->success('登录成功', url('Teachers/index'));
        } else {
            // 用户名不存在,跳转到登录界面。
            return $this->error('用户名或密码错误', url('index'));
        }
    }

}

效果:(用户名root,密码root)

thinkphp5中的登录验证_第2张图片

方法二

我们说登录是教师这个类的登录。按照分层原则,应该写到教师这个类中。即把验证的数据交给teacher这个模型类去向数据库作验证,这样就避免在控制器类里引入模型类,然后实例化模型类去验证。

Login.php(控制器类)

public function login()
    {
        // 接收post信息
        $postData = Request::instance()->post();

        // 直接调用M层方法,进行登录。
        if (Teacher::login($postData['username'], $postData['password'])) {
            return $this->success('login success', url('Teacher/index'));
        } else {
            return $this->error('username or password incorrent', url('index'));
        }
    }

teacher.php(模型类)

这里假设经过查询后的用户名和密码正确,所以返回真,具体与数据库的交互看代码

static public function login($username, $password)
    {
        return true;
    }
//这样的话,不管你输入什么,都能登录。

二.验证

方法一--使用增加方法验证

teachers控制器里的index方法增加session查询:

        // 验证用户是否登录
        $teacherId = session('teacherId');
        if ($teacherId === null)
        {
            return $this->error('plz login first', url('Login/index'));
        }

或者与登录一样,放到Teacher模型类的做,这里假设成功登录

 static public function isLogin()
    {
        return true;
    }

  teachers.php  下index()

 // 验证用户是否登录
        if (!Teacher::isLogin()) {
            return $this->error('plz login first', url('Login/index'));
        }

注意:这里会出现一个错误,你第一次登录成功后,第二次访问Teacher主页是不需要登录直接可以访问,这是因为第一次登录时框架自动生成了session,你换个浏览器或者做个退出按钮清理session

下面是我第一次登录成功后的情况:

thinkphp5中的登录验证_第3张图片

增加一个退出

thinkphp5中的登录验证_第4张图片

方法二--构造函数

在控制器里增加构造函数

public function __construct()
    {
        // 调用父类构造函数(必须)
        parent::__construct();
        
        // 验证用户是否登陆
        if (!Teacher::isLogin()) {
            return $this->error('plz login first', url('Login/index'));
        }
    }

方法三--

thinkphp5中的登录验证_第5张图片

 

 1、删除Teacher类中的构造函数;
2、Teacher类由继承于think\Controller改为继承于IndexController;
3、在IndexController中新建构造函数,进行用户是否登录的判断。

IndexController

namespace app\index\controller;
use app\common\model\Teacher;
use think\Controller;

class IndexController extends Controller
{
    public function __construct()
    {
        // 调用父类构造函数(必须)
        parent::__construct();

        // 验证用户是否登陆
        if (!Teacher::isLogin()) {
            return $this->error('plz login first', url('Login/index'));
        }
    }
}

 

具体代码:https://github.com/lpx20181019/php_advanced/tree/master/tp5.0.24--%E7%BB%A7%E6%89%BF%E9%AA%8C%E8%AF%81

你可能感兴趣的:(thinkphp5中的登录验证)