drlh197610 2016-12-30 02:39
浏览 96
已采纳

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

How do i redirect the user into current page after login?

User is reading a news at this URL domain.com/article/news-18565 once he click the login button then how do i get currenturl() before going to domain.com/login and after login user should go to domain.com/article/news-18565

Form.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>

Controller.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');
        }
    }
 }

Model.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;
    }

}
  • 写回答

1条回答 默认 最新

  • dpt8910 2016-12-30 10:20
    关注

    well you need use HTTP_REFERER url. http Referer url redirect user to last visited url as you say

    E.g: Member is reading a news at this URL domain.com/article/news-18565 once he click the login button then how to get currenturl() before going to domain.com/login and after login member should go to domain.com/article/news-18565

    For this you need check the last visited url see in code example.

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

    Hope it's help you if you have any question let me know

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容