TP5.1 登录 配置是否登录公共函数的方法(完整的登录案例)
Posted Q神
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TP5.1 登录 配置是否登录公共函数的方法(完整的登录案例)相关的知识,希望对你有一定的参考价值。
配置好后,就不需要每个控制器都需要加一个是否检查登录的函数
1、在\\application\\admin\\controller下创建Common.php
namespace app\\admin\\controller;
use think\\Controller;
use think\\Request;
use think\\Db;
class Common extends Controller
//检查是否登录
public function _initialize()
if(!session(username))
$this->error(请先登录!,url(/admin/login/index));
注意:
在这里已经继承了控制器(class Common extends Controller),其它控制继承的就不是控制器了,而且是Common
2、其它控制文件,比如:在\\application\\admin\\controller下的文件 admin.php
namespace app\\admin\\controller;
//use think\\Controller;
use think\\Validate;
//use think\\Request;
use think\\Db;
class Admin extends Common
完整的登录案例:
Login.php控制器代码
<?php
namespace app\\admin\\controller;
use think\\Controller;
use think\\Validate;
use think\\Request;
use think\\Db;
use think\\Session;
class Login extends Controller
public function index()
return $this -> fetch();
public function login()
$username=input(username);
$password1=input(password);
if(!$username || !$password1)
$this->error(错误:用户名、密码不能为空);
$cms_password = cms_password;
if(request()->isPost())
$this->check(input(code));
$password=md5($password1.$cms_password);
if($username)//验证用户名
$rsu= Db::name(admin)->where(name,$username)->find();
$countu = count($rsu);
if(!$countu)
$s1=1;//=1失败
else
$s1=9;//=9成功
if($s1==1)
$this->error(用户不存在!);
if($username && $password1)//验证密码
$rsp= Db::name(admin)->where(name,$username)->where(password,$password)->find();
$countp = count($rsp);
if(!$countp)
$s2=1;
else
$s2=9;
$id=$rsp[id];
$group_id=$rsp[group_id];//权限分组ID
$img=$rsp[img];//头像
if($s2==1)
$this->error(密码错误!);
if($s1==9 && $s2==9)
session(id, $id);
session(username, $username);
session(group_id, $group_id);
session(img, $img);
$this->success(登录成功!,url(/admin/index));
return;
return $this -> fetch();
// 验证码检测
public function check($code=)
if (!captcha_check($code))
$this->error(验证码错误);
else
return true;
//退出登录
public function logout()
session(null);
$this->success(登出成功!,url(/admin/login/index));
模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>后台管理登录页面</title>
<meta name="keywords" content="后台管理模版" />
<meta name="description" content="FnCMS后台管理模版" />
<meta name="Author" content="CMS" />
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="Shortcut Icon" href="/favicon.ico" />
<link rel="stylesheet" href="__STATIC__/layui/css/layui.css">
<link rel="stylesheet" type="text/css" href="__ADMIN__/login/login.css" media="all">
</head>
<body>
<div class="larry-main layui-layout animated shake larry-delay2" id="larry_login">
<div class="title">管理系统</div>
<p class="info">后台登录中心</p>
<div class="user-info">
<div class="avatar"><img src="__ADMIN__/login/admin.png" alt=""></div>
<form action=":url(login)" method="post" class="layui-form" id="larry_form">
<div class="layui-form-item">
<label class="layui-form-label">用户名:</label>
<input type="text" name="username" required lay-verify="required" aautocomplete="off" class="layui-input larry-input" placeholder="请输入您的用户名">
</div>
<div class="layui-form-item" id="password">
<label class="layui-form-label">密码:</label>
<input type="password" name="password" required lay-verify="required|password" aautocomplete="off" class="layui-input larry-input" placeholder="请输入您的登录密码">
</div>
<div class="layui-form-item larry-verfiy-code" id="larry_code">
<input type="text" name="code" required lay-verify="required" aautocomplete="off" class="layui-input larry-input" placeholder="输入验证码">
<div class="code">
<div style="width: 270px; height: 55px;"><img src=":captcha_src()" onClick="this.src=:captcha_src()?+Math.random();" alt="captcha" style="width: 178px; height: 36px; padding: 1px 0px 0px 2px;"/></div>
</div>
</div>
<div class="layui-form-item">
<button class="layui-btn larry-btn" lay-filter="submit" lay-submit>立即登录</button>
</div>
</form>
</div>
<div class="copy-right">© 2016-2017 xxxx 版权所有 <a href="http://www.xxxxx.com" target="_blank">xxxx.com</a></div>
</div>
<script type="text/javascript" src="__STATIC__/layui/layui.js"></script>
<script type="text/javascript">
layui.use(["jquery", "layer", "form"], function()
var layer = layui.layer
,form = layui.form
,$= layui.$;
var n = ($(document).height()-$("#larry_login").height())/2.5;
$("#larry_login").css(
"margin-top": n
);
);
</script>
</body>
</html>
初始化方法
在\\application\\admin\\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(username) || !session(id))
$this->error(请先登录!,url(/admin/login/index));
以上是关于TP5.1 登录 配置是否登录公共函数的方法(完整的登录案例)的主要内容,如果未能解决你的问题,请参考以下文章