2016-12-30 69 views
0

我知道这个概念的会话和用户代理库,我很困惑如何实现。登录后将成员重定向到当前页面的最佳方式(Codeigniter)

例如:会员在此URL domain.com/article/news-18565阅读的新闻,一旦他才去domain.com/login和登录会员后应该去domain.com/article/news-18565

Form.html

点击登录按钮,然后怎么去CURRENTURL()
<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; 
    } 

} 

回答

0

以及你需要使用HTTP_REFERER网址。 HTTP引用URL重定向用户到最后访问的URL,你说

例如:会员在读这个URL domain.com/article/news-18565新闻一旦他点击登录按钮,然后怎么去CURRENTURL()在进入domain.com/login之前以及登录成员之后,请转到domain.com/article/news-18565

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

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

希望这是帮助你,如果你有任何问题,让我知道

相关问题