登录后如何将用户重定向到当前页面(Codeigniter)

Fay*_*Fay 1 php login codeigniter codeigniter-3

登录后如何将用户重定向到当前页面?

用户在domain.com/article/news-18565点击登录按钮后正在此 URL 上阅读新闻,然后我如何domain.com/login在登录用户之前和之后获取 currenturl()domain.com/article/news-18565

表单.html

<form action="signin" method="post" accept-charset="utf-8">
        <div class="form-group">
            <label for="">Email:</label>
            <input type="text" name="email" value="" class="form-control form-control-lg">
        </div>

        <div class="form-group">
            <label for="">Password:</label>
            <input type="password" name="password" value="" class="form-control form-control-lg">
        </div>

        <div class="form-group">
            <input type="submit" name="submit" value="Sign in to your account" class="float-md-right btn btn-primary">
        </div>
    </form>
Run Code Online (Sandbox Code Playgroud)

控制器.php

    function index(){

    $user_profile = 'user';
    $this->authModel->loggedin() == FALSE || redirect($user_profile);

    $rules = $this->authModel->rules;
    $this->form_validation->set_rules($rules);
    if($this->form_validation->run() == TRUE){
        if($this->authModel->login() == TRUE){
            redirect($user_profile);
        }else{
            $this->session->set_flashdata('error', 'That email/password combination does not exist');
            redirect('signin');
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)

模型.php

    function login(){

    $user = $this->get_by(array(
            'email' => $this->input->post('email', TRUE),
            'password' => $this->hash($this->input->post('password'))
        ), TRUE);

    if(count($user)){
        $data = array(
            'name' => $user->name,
            'email' => $user->email,
            'username' => $user->username,
            'loggedin' => TRUE
        );
        $this->session->set_userdata($data);
        return TRUE;
    }

}
Run Code Online (Sandbox Code Playgroud)

Yas*_*mad 6

好吧,您需要使用 HTTP_REFERER 网址。如您所说,http Referer url 将用户重定向到上次访问的 url

例如:会员在点击登录按钮后正在此 URL domain.com/article/news-18565 阅读新闻,然后如何在访问 domain.com/login 之前获取 currenturl() 以及登录后会员应访问 domain.com /文章/新闻-18565

为此,您需要检查上次访问的 url,请参见代码示例。

$this->session->set_userdata($data);
if(!empty($_SERVER['HTTP_REFERER'])){
    redirect($_SERVER['HTTP_REFERER']);
} else {
   // add here the default url for example dashboard url
}
Run Code Online (Sandbox Code Playgroud)

如果您有任何问题,请告诉我,希望对您有所帮助