本文主要是介绍tp5-登录验证(单一入口),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
控制器 Common.php
<?php
namespace app\admin\controller;
use think\Controller;class Common extends Controller
{public function _initialize(){if (!session('id') || !session('name')) {$this::error("您尚未登录系统!",url('login/login'));}} }
控制器 Login.php
<?php
namespace app\admin\controller;
use think\Db;
use think\Controller;
use app\admin\model\Login as LoginModel;class Login extends Controller
{public function login(){ return view();}public function index(){if (request()->isPost()) {$admin = new LoginModel();$num = $admin->login(input("post."));if ($num == 1) {$this::error("用户不存在,请检查!");}if($num == 2){$this::success("登录成功~",url('user/index'));}if ($num == 3) {$this::error("密码错误!请检查~");}return;}return view("login");}public function layout(){session(null);$this::success("退出成功!",url('login/login'));}
}
model Login.php
<?php
namespace app\admin\model;
use think\Model;
use think\Db;
class Login extends Model
{public function login($data){$user = db('user')->where('name',$data['name'])->find();if ($user) {if ($user['password'] == md5($data['password'])) {session('id',$user['id']);session('name',$user['name']);return 2;}else{return 3;}}else{return 1;}}
}
控制器(Login.php 除外)继承 Common 就可以实现单一入口
这篇关于tp5-登录验证(单一入口)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!