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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Solution to the error reported when deploying thinkphp5 in Pagoda: 1. Open the Pagoda server, install the php pathinfo extension and enable it; 2. Configure the ".access" file with the content "RewriteRule ^(.*)$ index.php?s=/$1 [QSA ,PT,L]”; 3. In website management, just enable thinkphp’s pseudo-static.

Solution to thinkphp5 url rewriting not working: 1. Check whether the mod_rewrite.so module is loaded in the httpd.conf configuration file; 2. Change None in AllowOverride None to All; 3. Modify the Apache configuration file .htaccess to "RewriteRule ^ (.*)$ index.php [L,E=PATH_INFO:$1]" and save it.

How to remove the thinkphp5 title bar icon: 1. Find the favicon.ico file under the thinkphp5 framework public; 2. Delete the file or choose another picture to rename it to favicon.ico and replace the original favicon.ico file.

thinkphp5 post cannot get a value because TP5 uses the strpos function to find the app/json string in the content-type value of the Header. The solution is to set the content-type value of the Header to app/json.

Methods for thinkphp5 to obtain the requested URL: 1. Use the "$request = Request::instance();" method of the "\think\Request" class to obtain the current URL information; 2. Use the built-in helper function "$request-> url()" to obtain the complete URL address including the domain name.

Solution to thinkphp5 prompting that the controller does not exist: 1. Check whether the namespace in the corresponding controller is written correctly and change it to the correct namespace; 2. Open the corresponding tp file and modify the class name.

How to query yesterday's data in ThinkPHP5: 1. Open ThinkPHP5 related files; 2. Query yesterday's data through the expression "db('table')->whereTime('c_time', 'yesterday')->select();" Can.

How to set error prompts in thinkphp5: 1. Enter the public folder in the project root directory and open the index.php entry file; 2. View the comments on the debug mode switch; 3. Adjust the value of the "APP_DEBUG" constant to true to display Error message prompt.
