登陆验证实现

原创 2018-12-24 16:11:14 205
摘要:<?php namespace app\admin\controller; use app\admin\model\UserModel; use think\App; use think\Controller; use think\facade\Request; use think\facade\Session; class&nb
<?php
namespace app\admin\controller;

use app\admin\model\UserModel;
use think\App;
use think\Controller;
use think\facade\Request;
use think\facade\Session;
class Login extends Controller
{
    public function  login()
    {
        //渲染模版
        return $this->fetch();
    }

    public function DoLogin()
    {   //$data用来存前台获取的数据
        $data = Request::param();
        $username = $data['username'];
        $user = UserModel::where('username',$username)->find();

        if($user != true)
        {
            $info = ['res' => 0,'msg' =>'用户名不存在'];
        }
        elseif ($data['password'] != $user['password'])
        {
            $info = ['res' => 0,'msg' => '密码错误'];
        } else {
            $info = ['res' => 1,'msg' => '登陆成功'];
            Session::set('username',$user['username']);
        }
        return $info;
    }

    public function LoginOut()
    {
        Session::delete('username');
        $this->redirect('login');
    }
}

//Commom.php

<?php

namespace app\admin\controller;

use think\Controller;
use think\facade\Session;
class Common extends Controller
{
    public function __construct()
    {
        parent::__construct();
        if(!Session::has('username'))
        {
            $this->error('未登录,请先登录','Login/login'); //跳转到Login下的Login方法
        }
    }
}


批改老师:韦小宝批改时间:2018-12-24 16:19:42
老师总结:写的很不错哦!这个项目写完了可以写个自己的博客玩玩也是很不错的哦!

发布手记

热门词条