2016-12-30 75 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 
} 

希望這是幫助你,如果你有任何問題,讓我知道

相關問題