当前位置:网站首页>Tp5.1 login configuration method of whether to login public functions (complete login case)

Tp5.1 login configuration method of whether to login public functions (complete login case)

2022-07-25 02:34:00 51CTO


After configuration , You don't need to add a function to check login for each controller

1、 stay \application\admin\controller Create Common.php

      
      
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Db;

class Common extends Controller
{
// Check if you are logged in
public function _initialize()
{
if(!session('username')){
$this->error(' Please log in first !',url('/admin/login/index'));
}
}

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

Be careful :

The controller has been inherited here (class Common extends Controller), Other controls do not inherit controllers , And it's Common


2、 Other control documents , such as : stay \application\admin\controller The files under the  admin.php

TP5.1 Sign in Configure the method of logging in public functions ( Complete login case )_ico

      
      
namespace app\admin\controller;
//use think\Controller;
use think\Validate;
//use think\Request;
use think\Db;

class Admin extends Common{
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.



Complete login case :

Login.php Controller code

      
      
<?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(' error : user name 、 The password cannot be empty ');
}
$cms_password = cms_password;
if(request()->isPost()){

$this->check(input('code'));

$password=md5($password1.$cms_password);

if($username){// Verify user name
$rsu= Db::name('admin')->where('name',$username)->find();
$countu = count($rsu);
if(!$countu){
$s1=1;//=1 Failure
}else{
$s1=9;//=9 success
}
}
if($s1==1){
$this->error(' The user doesn't exist !');
}
if($username && $password1){// Verify password
$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'];// Permission grouping ID
$img=$rsp['img'];// Head portrait
}
}
if($s2==1){
$this->error(' Wrong password !');
}
if($s1==9 && $s2==9){
session('id', $id);
session('username', $username);
session('group_id', $group_id);
session('img', $img);
$this->success(' Login successful !',url('/admin/index'));
}
return;
}
return $this -> fetch();
}

// Captcha detection
public function check($code='')
{
if (!captcha_check($code)) {
$this->error(' Verification code error ');
} else {
return true;
}
}

// Log out
public function logout()
{
session(null);
$this->success(' Log out successfully !',url('/admin/login/index'));
}


}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.


Templates

      
      
<!DOCTYPE html>
< html lang= "en" >
< head >
< meta charset= "UTF-8" >
< title > Background management login page </ title >
< meta name= "keywords" content= " Background management template " />
< meta name= "description" content= "FnCMS Background management template " />
< 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" > Management system </ div >
< p class= "info" > Background login Center </ 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" > user name : </ label >
< input type= "text" name= "username" required lay-verify= "required" aautocomplete= "off" class= "layui-input larry-input" placeholder= " Please enter your user name " >
</ div >
< div class= "layui-form-item" id= "password" >
< label class= "layui-form-label" > password : </ label >
< input type= "password" name= "password" required lay-verify= "required|password" aautocomplete= "off" class= "layui-input larry-input" placeholder= " Please enter your login password " >
</ 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= " Enter verification code " >
< 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 > Login immediately </ button >
</ div >
</ form >
</ div >
< div class= "copy-right" > 2016-2017 xxxx copyright < 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 >
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.


Initialization method

stay \application\admin\controller Create Common.php

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

class Common extends Controller
{
// Check if you are logged in
public function initialize()
{
if( ! session( 'username') || ! session( 'id')){
$this -> error( ' Please log in first !', url( '/admin/login/index'));
}
}

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.



原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/202/202207200759156657.html