tp5判断是否登录

在这里插入图片描述
Controller控制器下新建Common.php

<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Db;

class Common extends Controller
{
    //检查是否登录
    public function _initialize()
    {
        if(!session('uname')){
            $this->error('请先登录!',url('/admin/login/index'));
        }
    }

}

在需要判断登录的类中继承该类:
Index.php

<?php
namespace app\admin\controller;
/**
 * Created by PhpStorm.
 * User: gao
 * Date: 2019/6/11
 * Time: 15:39
 */
use think\Controller;
use think\Db;

class Index extends Common
{

    //框架应用
    public function Index(){
        return view("index");
    }
    /**
     * 文章内容显示
     */
    public function AList(){

        $sou = request()->param();
        $name = input('tid') ? input('title') : ($sou['title'] = '');
        //判断是不是第一栏目
        $t_fid = input('t_fid') ? input('t_fid') : 0;
        $Z_tid = 2;

        if(isset($_POST['sousuo'])){
            $tid = input("tid");
            $title = input("title");
            $data = array();
            if(!empty($tid)){
                $data['a.tid'] = $tid;
            }
            if(!empty($title)){
                $data['title'] = ['like','%'.$title.'%'];
            }
            $arr=Db::table('article')
                ->alias('a')
                ->join('type t','a.tid=t.tid')
                ->field('a.*,t.tname')//查询指定字段
                ->where('a.tid','neq',$Z_tid)
                ->where($data)
                ->group('a.id')//分组
                ->paginate(5, false, ['query' => request()->param()]);//分页
            $page=$arr->render();
            $tid = Db::name('type')->select();
            return view("list",['arr'=>$arr,'tid'=>$tid,'page'=>$page,'title'=>$title]);
        }else{
            $arr=Db::table('article')
                ->alias('a')
                ->join('type t','a.tid=t.tid')
                ->field('a.*,t.tname')//查询指定字段
                ->where('a.tid','neq',$Z_tid)
                ->order("id desc")
                ->group('a.id')//分组
                ->paginate(5, false, ['query' => request()->param()]);//分页

            $page=$arr->render();
            $tid = Db::name('type') ->where('tid','neq',$Z_tid)->select();
            return view("list",['arr'=>$arr,'tid'=>$tid,'page'=>$page,'title'=>""]);
        }
    }


    /**
     * 文章类型删除
     */
    public function type_delete(){
        $id=$_GET['id'];
        $rs=Db::table('type')->where('tid',$id)->delete();
        if($rs)
        { $this->success('删除成功','admin/Index/typeContent');}
        else
        {  $this->error('删除失败','admin/Index/typeContent');}
    }

    /**
     * 文章类型修改
     */
    public function type_update()
    {

        $id = input('tid');
        if (isset($_POST['sub'])) {
            $tname = input("tname");
            $xiugai = Db::name('type')
                ->where('tid', $id)
                ->update(['tname' => $tname]);
            if ($xiugai)
                $this->success('修改成功', 'admin/Index/typeContent');
            else
                $this->error('修改失败', 'admin/Index/typeContent');

        }else{
            $id = input('id');
            $array = Db::name('type')->where('tid',$id)->find();
            return view("type_update",['arr'=>$array]);
        }
    }
}

在不需要判断是否登录的类中不继承该类,继承Controller就行:
Login.php

<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Login extends Controller{
    public function index(){
            if(null!=input('submit'))
            {
                $name=input('name');
                $pwd=input('pass');
                $user=Db::table('admin')
                    ->where('uname',$name)
                    ->where('upwd',$pwd)
                    ->find();
                if($user)
                {
                    Session::set('uname',$name);
                    $this->success('登陆成功','admin/Index/index');
                }
                else
                {
                    $this->error('用户名或密码错误','index');
                }
            }else{
                return view('login');
            }
    }
    function dieLogin()
    {
        Session(null);
        return view('login');
    }
}
发布了328 篇原创文章 · 获赞 110 · 访问量 47万+

猜你喜欢

转载自blog.csdn.net/qq_42249896/article/details/93160663