摘要:<?phpnamespace app\admin\controller;use think\Controller;use think\facade\Session;//公共类 通过session判断有没有登录 其他类继承//session如果写在普通方法里在其他类的方法中需要调用//写在构造方法中继承后就运行了 不需再次调用class Common extends Controller{&n
<?php
namespace app\admin\controller;
use think\Controller;
use think\facade\Session;
//公共类 通过session判断有没有登录 其他类继承
//session如果写在普通方法里在其他类的方法中需要调用
//写在构造方法中继承后就运行了 不需再次调用
class Common extends Controller
{
public function __construct()
{
parent::__construct();
if(!Session::has('username')){
$this->error('没有登陆',url('login/index'));
}
}
}
<?php
namespace app\admin\controller;
use think\Controller;
use think\facade\Request;
use app\admin\model\UserModel;
use think\facade\Session;
class Login extends Controller
{
public function index()
{
//输出登录页面
return $this->fetch();
}
//登录验证处理
public function login(){
$data = Request::param();
$username = $data['username'];
$res = UserModel::where('username',$username)->find();
if($res!=true){
$info = ['status'=>0,'msg'=>'用户名不存在'];
}elseif($data['password']!=$res['password']){
$info = ['status'=>0,'msg'=>'密码错误'];
}else{
$info = ['status'=>1,'msg'=>'登陆成功'];
Session::set('username',$res['username']);
}
return $info;
}
//退出登录
public function loginOut(){
//删除session
Session::delete('username');
//跳转到登录页
$this->success('退出',url('index'));
}
}
批改老师:韦小宝批改时间:2018-11-03 09:15:53
老师总结:不错啊!这进度很快嘛!赞一个!继续加油吧!