Home > Article > PHP Framework > How to implement backend login interface in thinkphp5
1. Unzip the "tp5" compressed package to "thinkphp_5.0.24_with_extend\"(E);
2. Put the unzipped "tp5 folder"— >Rename "demo (you can give other names)"->Copy the demo folder to the WWW directory;
3. Enter "http://127.0.0.1/demo/public" in the browser "—> Check whether tp5 can be used;
4. Create or import a database (I imported it);
5. In the application folder—> Create the admin folder —>In the admin folder—>Create controller,
model, and view folders respectively—>In the controller folder—>Create Login.php;
D: \phpStudy\WWW\demo\application\admin\controller\Login.php
Content
<?php namespace app\admin\controller; use think\Controller; use app\admin\model\Login as Log; class Login extends Controller { public function index() { // $linkres= \think\Db::name('link')->paginate(3); // $this->assign('linkres',$linkres); if(request()->isPost()){ $login=new Log; $status=$login->login(input('username'),input('password')); if($status==1){ return $this->success('登录成功,正在跳转!','Index/index'); }elseif($status==2){ return $this->error('账号或者密码错误!'); }else{ return $this->error('用户不存在!'); } } return $this->fetch('login'); } public function logout(){ session(null); return $this->success('退出成功!',url('index')); } }
6. In the model folder—>Create the Login.php file
D:\phpStudy\WWW\demo\application\admin\model\Login.php
Content:
<?php namespace app\admin\model; use think\Model; class Login extends Model { public function login($username,$password){ $admin= \think\Db::name('admin')->where('username','=',$username)->find(); if($admin){ if($admin['password']==md5($password)){ \think\Session::set('id',$admin['id']); \think\Session::set('username',$admin['username']); return 1; }else{ return 2; } }else{ return 3; } } }
7. In the view folder—>Create the Login folder—> ;In the Login folder—>Create the login.html file
D:\phpStudy\WWW\demo\application\admin\view\Login\login.html
Content:
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>后台登录</title> <link href="__PUBLIC__/static/admin/css/admin_login.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="admin_login_wrap"> <h1>后台管理</h1> <div class="adming_login_border"> <div class="admin_input"> <form action="" method="post"> <ul class="admin_items"> <li> <label for="user">用户名:</label> <input type="text" name="username" value="admin" id="user" size="35" class="admin_input_style" /> </li> <li> <label for="pwd">密码:</label> <input type="password" name="password" value="admin" id="pwd" size="35" class="admin_input_style" /> </li> <li> <input type="submit" tabindex="3" value="提交" class="btn btn-primary" /> </li> </ul> </form> </div> </div> </div> </body> </html>
8.D:\phpStudy\WWW\demo\application\config.php
// 应用调试模式 'app_debug' => false,
Modify it to:
'app_debug' => true,
and you will see the bug!
The template file does not exist: D:\phpStudy\WWW\demo\public/../application/admin\view\login\login.html
The login file name under view is wrong! ! ! *Use uppercase Login in Login.php under controller and model, and lowercase Login in login.html under view!
SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO) The bug appears because there is no connection to the database
D:\phpStudy\WWW\ demo\application\database.php
Fill in the content
return [ // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => '127.0.0.1', // 数据库名 'database' => 'youme', //你创建或导入的数据库名 // 用户名 'username' => 'root', // 密码 'password' => '****', // 端口 'hostport' => '', // 连接dsn 'dsn' => '', // 数据库连接参数 'params' => [], // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库表前缀 'prefix' => 'ym_', // 你创建或导入数据库表名的前缀 ***************************************************************************************** SQLSTATE[42S02]: Base table or view not found: 1146 Table 'youhe.admin' doesn't exist(Bug)
D:\phpStudy\WWW\demo\application\admin\model\Login.php
<?php namespace app\admin\model; use think\Model; class Login extends Model { public function login($username,$password){ // $admin= \think\Db::name('admin')->where('username','=',$username)->find(); $user= \think\Db::name('user')->where('username','=',$username)->find(); // if($admin){ if($user){ // if($admin['password']==md5($password)){ if($user['password']==$password){ // \think\Session::set('id',$admin['id']); \think\Session::set('id',$user['id']); // \think\Session::set('username',$admin['username']); \think\Session::set('username',$user['username']); return 1; }else{ return 2; } }else{ return 3; } } }
Recommended tutorial: 《
TP5The above is the detailed content of How to implement backend login interface in thinkphp5. For more information, please follow other related articles on the PHP Chinese website!