Home  >  Article  >  PHP Framework  >  How thinkphp sets session verification on each page

How thinkphp sets session verification on each page

PHPz
PHPzOriginal
2023-04-11 15:10:09909browse

With the rapid development of the Internet, developers are constantly exploring new technologies and frameworks. One of the most popular frameworks is thinkphp. thinkphp is an efficient, fast and high-performance PHP framework that can greatly improve efficiency during the development process and also has good scalability and openness. In the thinkphp framework, session is an indispensable part, especially in user login verification and identity authentication. This article will introduce how to set up session verification on each page.

1. Using session in thinkphp framework

Session is a technology used to store users’ temporary information on the website. In the thinkphp framework, using session is very simple. You only need to execute the following code:

session_start(); //开启session
$_SESSION['key'] = 'value'; //设置session变量

Of course, you can also use the session class provided in the thinkphp framework to operate. The specific code is as follows:

use think\facade\Session;
Session::set('key', 'value');

2. Automatically verify whether the session exists

In order to ensure the security of the system, we need to verify whether the user has logged in. In the thinkphp framework, you can use the middleware mechanism to perform session verification on each page to achieve automatic verification. The specific code is as follows:

namespace app\http\middleware;

use Closure;
use think\facade\Session;
use think\exception\HttpException;

class CheckLogin
{
    public function handle($request, Closure $next)
    {
        if (!Session::has('user_id')) {
            throw new HttpException(401, '请先登录');
        }

        return $next($request);
    }
}

In the above code, the namespace namespace is first used to define the location where the custom application middleware is used, that is, in the "app\http\middleware" directory. In the handle() method, first determine whether the user is logged in by determining whether the user_id variable exists in the session. If there is no login, an HTTP exception will be thrown to prompt the user to log in.

3. Using middleware in Controller

In the thinkphp framework, you can add corresponding checks by calling middleware in the constructor of Controller to verify whether the user is logged in normally. The specific code is as follows:

namespace app\controller;

use app\http\middleware\CheckLogin;
use think\Controller;

class Index extends Controller
{
    protected $middleware = [
        CheckLogin::class,
    ];

    //index方法省略
}

In the above code, the namespace is used for definition. The Controller class inherits the parent class in the think framework and defines a middleware CheckLogin, which means that CheckLogin is introduced in the Controller. Middleware to check the user's login identity.

This article mainly introduces the use of session in the thinkphp framework to verify whether the user is logged in, and how to use the middleware mechanism to achieve automatic verification of each page. If you want to master the application of session proficiently, you still need a long process of practice and learning. I hope this article can provide readers with some help and reference in their study and practice.

The above is the detailed content of How thinkphp sets session verification on each page. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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