think php 未登录,禁止访问页面 + 退出登录
1.首先在Local创建一个基类控制器
D:\PHP\phpstudy_pro\WWW\1906A\pyg>php think make:controller goods/Base --plain
Controller created successfully.
2:判断页面是否有session值,,没有就跳转会登录页面,登录后进行访问
<?php namespace app\goods\controller; use think\Controller; use think\Request; class Base extends Controller { // public function __construct(Request $request = null) { parent::__construct($request); if (!session('?name')){ $this->error('您还没有登录,请登录后在进行访问.........','/goods/login/create'); } } }
3:
、、、、、、、、表单页面的退出登录 <li><a tabindex="-1" href="/goods/login/loginOut">安全退出</a></li>
<?php namespace app\goods\controller; use app\goods\model\GoodModel; use think\Controller; use think\Loader; use think\Request; use think\Validate; class Login extends Controller { //展示登录表单 public function create() { //goods/login/create //通过域名/模块名/控制器名/方法名访问登录页面 return view(); } //登录处理,验证参数 public function save(Request $request) { // $params = $request->param(); //验证参数、非空 $rule = [ 'account' => 'require', 'password' => 'require', 'cord' => 'require', ]; $tips = [ 'account.require' => '账号不可以为空', 'password.require' => '密码不可以为空', 'cord.require' => '验证码不可以为空', ]; $validate = new Validate($rule, $tips); $result = $validate->check($params); if (!$result) { $this->error($validate->getError()); } //数据库验证参数 $data = GoodModel::login($params); //验证账号 if ($params['account'] != $data['name']) { $this->error('账号错误', '/goods/login/create'); } //验证密码 if (md5($params['password']) != md5($data['password'])) { $this->error('密码错误', '/goods/login/create'); } //验证码进行验证 if (!captcha_check($params['cord'])) { $this->error('验证码输入错误', '/goods/login/create'); }; if ($data) { //记录session session('name', $data['name']); $this->success('登录成功', '/goods/goods/index'); } } //退出登录 public function loginOut() { //清除session session('name',null); //返回登录页面 $this->success('正在退出,请稍等','/goods/login/create'); } }
//////////////////////表单登录页面
<!DOCTYPE html> {__NOLAYOUT__} <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>登录</title> <link rel="stylesheet" href="__STATIC__/admin/css/login.css"> </head> <body> <form action="/goods/login/save" method="post"> <div class="login"> <div class="center"> <h1>Login</h1> <div class="inputLi"> <strong>账户</strong> <input type="text" placeholder="账户" name="account"> </div> <div class="inputLi"> <strong>密码</strong> <input type="password" placeholder="密码" name="password"> </div> <div class="inputLi"> <strong>验证码</strong> <input type="text" placeholder="验证码" name="cord"> <img src="{:captcha_src()}" name="img" onclick="this.src='{:captcha_src()}'" > </div> <div class="inputLi"> <button type="submit">登录</button> </div> </div> </div> </form> </body> </html>