登录后重定向到上一页,不使用查询字符串


Redirect to previous page after logged in without using query string

有没有另一种方法可以在用户登录后重定向到上一页,而无需存储查询字符串的重定向 URL。我只看到了使用查询字符串的示例,但我想在不存储查询字符串的重定向 URL 的情况下实现这一点。提前感谢!

您可以像这样将其存储在会话中,而不是将重定向 URL 存储在查询字符串中:

$_SESSION['login-redirect'] = '/some-path.php'

然后,您可以像这样重定向:

$loginRedirectUrl = '/default-path.php'
if (isset($_SESSION['login-redirect'])) $loginRedirectUrl = $_SESSION['login-redirect'];
header('Location: '.$loginRedirectUrl);

在控制器中:

 public function product()
 {
     if ($this->session->userdata('logged_in')) {
        $data["refined_product_category"] = $this->cmsdbmodel->refined_product();
        $this->load->view('cms/header');
        $this->load->view('cms/products',$data);
        $this->load->view('cms/footer');
     }
     else {
        $url = current_url();  //gets your current url.
        $this->session->set_userdata("sess_url", $url);  //keeps your current url in session.
        $this->index();
     }
}

然后在登录页面中。

public function login()
{
   if (login success) {
      redirect($this->session->userdata("sess_url"));  //retrieve the url that was in session and redirect.
   }
}

这样做了,它对我有用。