Home PHP Framework ThinkPHP How to implement backend login interface in thinkphp5

How to implement backend login interface in thinkphp5

May 15, 2020 am 09:15 AM
thinkphp5

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(&#39;link&#39;)->paginate(3);
        // $this->assign(&#39;linkres&#39;,$linkres);
        if(request()->isPost()){
            $login=new Log;
            $status=$login->login(input(&#39;username&#39;),input(&#39;password&#39;));
            if($status==1){
                return $this->success(&#39;登录成功,正在跳转!&#39;,&#39;Index/index&#39;);
            }elseif($status==2){
                return $this->error(&#39;账号或者密码错误!&#39;);
            }else{
                return $this->error(&#39;用户不存在!&#39;);
            }
        }
        return $this->fetch(&#39;login&#39;);
    }
 
    public function logout(){
        session(null);
        return $this->success(&#39;退出成功!&#39;,url(&#39;index&#39;));
    }
 
 
}
Copy after login

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(&#39;admin&#39;)->where(&#39;username&#39;,&#39;=&#39;,$username)->find();
        if($admin){
            if($admin[&#39;password&#39;]==md5($password)){
                \think\Session::set(&#39;id&#39;,$admin[&#39;id&#39;]);
                \think\Session::set(&#39;username&#39;,$admin[&#39;username&#39;]);
                return 1;
            }else{
                return 2;
            }
 
        }else{
            return 3;
        }
    }
}
Copy after login

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>
Copy after login

8.D:\phpStudy\WWW\demo\application\config.php

// 应用调试模式
 &#39;app_debug&#39;              => false,
Copy after login

Modify it to:

&#39;app_debug&#39;              => true,
Copy after login

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 [
    // 数据库类型
    &#39;type&#39;            => &#39;mysql&#39;,
    // 服务器地址
    &#39;hostname&#39;        => &#39;127.0.0.1&#39;,
    // 数据库名
    &#39;database&#39;        => &#39;youme&#39;, //你创建或导入的数据库名
    // 用户名
    &#39;username&#39;        => &#39;root&#39;,
    // 密码
    &#39;password&#39;        => &#39;****&#39;,
    // 端口
    &#39;hostport&#39;        => &#39;&#39;,
    // 连接dsn
    &#39;dsn&#39;             => &#39;&#39;,
    // 数据库连接参数
    &#39;params&#39;          => [],
    // 数据库编码默认采用utf8
    &#39;charset&#39;         => &#39;utf8&#39;,
    // 数据库表前缀
    &#39;prefix&#39;          => &#39;ym_&#39;,  // 你创建或导入数据库表名的前缀
 
 
*****************************************************************************************
  SQLSTATE[42S02]: Base table or view not found: 1146 Table &#39;youhe.admin&#39; doesn&#39;t exist(Bug)
Copy after login

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(&#39;admin&#39;)->where(&#39;username&#39;,&#39;=&#39;,$username)->find();
        $user= \think\Db::name(&#39;user&#39;)->where(&#39;username&#39;,&#39;=&#39;,$username)->find();
//        if($admin){
        if($user){
//            if($admin[&#39;password&#39;]==md5($password)){
            if($user[&#39;password&#39;]==$password){
//                \think\Session::set(&#39;id&#39;,$admin[&#39;id&#39;]);
                \think\Session::set(&#39;id&#39;,$user[&#39;id&#39;]);
//                \think\Session::set(&#39;username&#39;,$admin[&#39;username&#39;]);
                \think\Session::set(&#39;username&#39;,$user[&#39;username&#39;]);
                return 1;
            }else{
                return 2;
            }
        }else{
            return 3;
        }
    }
}
Copy after login

Recommended tutorial: 《

TP5

The 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
What should I do if I get an error when deploying thinkphp5 in Pagoda? What should I do if I get an error when deploying thinkphp5 in Pagoda? Dec 19, 2022 am 11:04 AM

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.

What should I do if thinkphp5 url rewriting fails? What should I do if thinkphp5 url rewriting fails? Dec 12, 2022 am 09:31 AM

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 thinkphp5 title bar icon How to remove thinkphp5 title bar icon Dec 20, 2022 am 09:24 AM

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.

What should I do if thinkphp5 post cannot get the value? What should I do if thinkphp5 post cannot get the value? Dec 06, 2022 am 09:29 AM

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.

How to get the requested URL in thinkphp5 How to get the requested URL in thinkphp5 Dec 20, 2022 am 09:48 AM

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.

What should I do if thinkphp5 prompts that the controller does not exist? What should I do if thinkphp5 prompts that the controller does not exist? Dec 06, 2022 am 10:43 AM

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 How to query yesterday's data in ThinkPHP5 Dec 05, 2022 am 09:20 AM

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 How to set error prompts in thinkphp5 Dec 07, 2022 am 10:31 AM

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.

See all articles