Home > PHP Framework > ThinkPHP > How to use thinkphp framework to implement login function

How to use thinkphp framework to implement login function

WBOY
Release: 2023-05-27 22:51:57
forward
1490 people have browsed it

Step One: Create a Login Page

To get started, you must first create a login page. The page needs to contain user and password fields, as well as a "Login" button. When building your page, you should use HTML and Bootstrap and define it in the view path. The following is an example:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Login</title>
    <link href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <h3>Login</h3>
        <form class="form-horizontal" role="form" method="post" action="/login/check">
            <div class="form-group">
                <label class="control-label col-sm-2" for="username">Username:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="username" placeholder="Enter username" name="username">
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-sm-2" for="password">Password:</label>
                <div class="col-sm-10">
                    <input type="password" class="form-control" id="password" placeholder="Enter password" name="password">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-default">Login</button>
                </div>
            </div>
        </form>
    </div>
</body>
</html>
Copy after login

Step 2: Create a controller Action

Create a controller Action to handle login requests. Users can verify whether they exist in the system by providing their username and password in this Action. When the username and password are valid, the user information should be stored in the session and the user should be redirected to the application home page. The following is a sample login check Action code:

<?php
namespace app\index\controller;

use think\Controller;
use think\Session;

class Login extends Controller
{
    public function index()
    {
        return view();
    }

    public function check($username, $password)
    {
        // 在此处使用您的逻辑来检测用户是否有效
        if ($username == &#39;admin&#39; && $password == &#39;password&#39;) {
            Session::set(&#39;username&#39;, $username);
            $this->redirect(&#39;/&#39;);
        } else {
            $this->error(&#39;Invalid username or password&#39;);
        }
    }
}
Copy after login

Step 3: Create a route

Finally, you need to create a path to the access controller in the route so that It can be called from the login page. Here is an example of a sample route:

<?php
use think\Route;

Route::rule(&#39;/&#39;, &#39;index/index&#39;);
Route::rule(&#39;/login&#39;, &#39;login/index&#39;);
Route::rule(&#39;/login/check&#39;, &#39;login/check&#39;);
Copy after login

In the above code, the '/login' access path displays the login page by pointing to the index method of the Login controller. Requests to log in will be handled by the check method of the Login controller, whose path is '/login/check'.

The above is the detailed content of How to use thinkphp framework to implement login function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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