简体   繁体   English

登录 Code Igniter 后如何重定向回上一页?

[英]How to redirect back to previous page after login in Code Igniter?

Okay, so this is what I do:好的,这就是我要做的:

  1. I go to www.mywebsite.com/orders?id=1我去www.mywebsite.com/orders?id=1

  2. It redirects be to login before proceeding.在继续之前,它会重定向到登录。

  3. I log in successfully but it redirects to www.mywebsite.com/orders .我登录成功,但它重定向到www.mywebsite.com/orders

If I am already logged in and go directly using GET method, it works fine.如果我已经登录并直接使用 GET 方法,它工作正常。 But if I am asked to login, the GET method disappears.但是如果我被要求登录,GET 方法就会消失。

How do I preserve ?id=1 ?我如何保留?id=1

Before redirecting the user back to the login page store the current page (the requested page) in a session variable.在将用户重定向回登录页面之前,将当前页面(请求的页面)存储在会话变量中。 Assuming you have a function called check_login this would more or less look like what you should do:假设您有一个名为check_login的函数,这或多或少看起来像您应该执行的操作:

public function check_login() {
    if (!$this->session->has_userdata('logged_in') || $this->session->logged_in !== true) {
        if (!empty($_SERVER['QUERY_STRING'])) {
            $uri = uri_string() . '?' . $_SERVER['QUERY_STRING'];
        } else {
            $uri = uri_string();
        }
        $this->session->set_userdata('redirect', $uri);
        redirect('/auth/login');
    }
}

Then when the user successfully logs in your login function should somewhere have the following logic:然后当用户成功登录你的登录功能时,应该有以下逻辑:

public function login() {

    // form validation
    // get post vars
    // check username/pwd against db
    if ($login) {

        if ($this->session->has_userdata('redirect')) {
            redirect($this->session->redirect);
        } else {
            redirect('/dashboard');
        }

    } else {
        // error logging in
    }

}

session variable could store the id.While log in using session pass the id value.You can retrive the value anywhere in session. session 变量可以存储 id。使用 session 登录时传递 id 值。您可以在 session 中的任何位置检索该值。

$this->load->library('session'); $this->load->library('session'); $this->session->set_userdata('userId', 'YourId'); $this->session->set_userdata('userId', 'YourId');

where userId would be the name of the session variable, and YourId would be the value.其中 userId 将是会话变量的名称,而 YourId 将是值。

只需使用此重定向($_SERVER['HTTP_REFERER']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM