本文实例讲述了thinkphp5框架实现登陆,登出及session登陆状态检测功能。分享给大家供大家
本文实例讲述了thinkphp 5框架实现登陆,登出及session登陆状态检测功能。分享给大家供大家参考,具体如下:
1,访问http://localhost/tp5/admin.php时,判断有没有登陆:
想法:写一个父类,继承controller,然后定义一个初始化方法,在控制器调用时就判断是否登陆
<?php
namespace app\Admin\controller;
use think\Controller;
use think\Session;
class Basic extends Controller
{
//如果你的控制器类继承了\think\Controller类的话,可以定义控制器初始化方法_initialize,在该控制器的方法调用之前首先执行。
public function _initialize()
{
//判断有无admin_username这个session,如果没有,跳转到登陆界面
if(!session('admin_username')){
return $this->error('您没有登陆',url('Login/login'));
}
}
}
登陆界面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>登陆</h1>
<div>
<form class="m-t" role="form" action="{url('Login/login')}" method="post">
<div class="input-field">
<input id="admin_username" type="text" name="admin_username" class=" validate" placeholder="请输入用户名" required>
<label for="admin_username">用户名</label>
</div>
<div class="input-field">
<input id="admin_password" type="password" name="admin_password" placeholder="请输入密码" class="validate" required>
<label for="admin_password">密码</label>
</div>
<button type="submit" class="waves-effect red lighten-1 waves-light btn full-width " style="margin-top:10px; height:40px;">登 录</button>
</form>
</div>
</body>
</html>
登陆控制器:
<?php
namespace app\Admin\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\Db;
class Login extends Controller{
public function login(){
//判断是否是post方法发送的数据:如果是则开始登陆
if (Request::instance()->isPost()){
$admin_username = input('post.admin_username');//接收前台用户名
$admin_password = input('post.admin_password');//接收前台密码
if(empty($admin_username) || empty($admin_password)){
$this->error("用户名或者密码不能为空!");
}
//从数据库读取数据
$admin_info = DB::name('admin')
->where('admin_username',$admin_username)
->find();
if(empty($admin_info)){
$this->error('用户不存在,请重新登陆',url('Login/login'));
}else{
if(md5($admin_password)!=$admin_info['admin_password']){
$this->error('密码不正确,请重新登陆',url('Login/login'));
}else{
Session::set('admin_username',$admin_username);
$this->success("登录成功!",url('Index/index'));
}
}
}else{//如果不是post,则返回登陆界面
return view('login');
}
}
public function logout(){
session(null);//退出清空session
return $this->success('退出成功',url('Login/login'));//跳转到登录页面
}
}
如果失败,回到登陆页面;如果登陆成功,跳转到后台首页:
<?php
namespace app\Admin\controller;
use think\Controller;
use think\Session;
use think\View;
//后台首页继承自登陆判断页面,每次加载都会先执行判断页面的代码
class Index extends Basic
{
public function index()
{
$view = new View();
$data = session('admin_username');
$view->assign('data',$data);
//return view('index');
return $view->fetch('index');
}
}
后台首页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!-- <h1>hello,{$Think.session.admin_username}</h1> -->
<h1>hello,{$data}</h1>
<a href="{:url('Login/logout')}" rel="external nofollow" >退出登陆</a>
</body>
</html>
thinkphp
5
登陆
登出
session
登陆状态检测