Home  >  Article  >  PHP Framework  >  How to jump after login in thinkphp

How to jump after login in thinkphp

PHPz
PHPzOriginal
2023-04-17 09:50:04753browse

In the development of the website, login is a very important function. When the user successfully logs in, it is also necessary to jump to the appropriate page so that the user can better use the website. When developing using the ThinkPHP framework, how to implement a jump after login? Let’s take a closer look below.

First, after successful login verification, we need to authenticate the user and write the user status to the session.

public function login()
{
    // 获取用户输入的用户名和密码
    $username = $this->request->param('username');
    $password = $this->request->param('password');
    // 对获取到的参数进行判断和处理
    if(empty($username) || empty($password)) {
        $this->error('用户名和密码不能为空');
    }
    // 查询用户信息
    $user = Db::name('user')->where('username',$username)->find();
    if(empty($user)) {
        $this->error('用户名不存在');
    }
    // 验证密码是否正确
    if(md5($password) !== $user['password']) {
        $this->error('密码错误');
    }
    // 将用户的信息写入 session
    session('user_id',$user['id']);
    session('user_info',$user);

    // 登录成功后进行跳转
    $this->redirect('/index');
}

In the above code, we first obtain the username and password entered by the user, then query whether the user exists, verify whether the password is correct, and if both are correct, write the user information to the session. Next, we can jump to the page through $this->redirect(). The

/index

in the <pre class="brush:php;toolbar:false">$this-&gt;redirect('/index');</pre> parameter indicates jumping to the index method in the root directory of the project, which is the default home page. If we want to jump to other pages, such as the shopping cart page, we only need to modify the parameters.

$this->redirect('/cart/index');

The above code means jumping to the index method under the Cart controller in the root directory.

In addition, we can also jump directly to a specific URL, but it should be noted that a relative path jump is required.

$this->redirect('/html/index.html');

Another thing to note is that the redirect() method performs 302 redirection by default. If we need to make a permanent jump, we can use parameters to specify it.

$this->redirect('/index',301);

The above is the method of jumping after login, taking the ThinkPHP framework as an example. In actual development, we also need to make modifications and adjustments according to our own needs. If you have any questions, you can leave a message in the comment area.

The above is the detailed content of How to jump after login in thinkphp. 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