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

How to implement backend login interface in thinkphp5

Release: 2020-05-15 09:15:49
forward
8845 people have browsed it

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!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template