2017-03-18 101 views
1

我有一個問題,我已經登錄頁面登錄,就會直接到配置文件,如果我們輸入了正確的電子郵件&密碼,但問題是,當我改變的URL登錄,它仍然移動到登錄頁面,我怎麼能阻止登錄頁面如果我已經登錄,這樣會dirrect到配置文件儘管該網址我改變它的登錄頁面仍然直接到配置文件頁面。個人資料頁還是移動到登錄頁面,如果我的網址改爲即使已經登錄

下面

是代碼:

class Profile extends CI_Controller { 

public function index() 
{ 
    if($this->session->userdata('logged_in')){ 
     $session_data = $this->session->userdata('logged_in'); 
     $data['email'] = $session_data['email']; 
     $this->load->view('view_profile', $data); 
    }else{ 
     redirect('login','refresh'); 
    } 
} 
public function logout(){ 
    $this->session->unset_userdata('logged_in'); 
    $this->session->sess_destroy(); 
    redirect(site_url('home'),'refresh'); 
} 

}

這是用戶數據( 'LOGGED_IN')

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class Home extends CI_Controller { 

    public function index() 
    { 
      $this->form_validation->set_rules('email','Email','trim|required'); 
      $this->form_validation->set_rules('password','Password','trim|required|callback_basisdata_cek'); 
      if($this->form_validation->run()==false){ 
       $this->load->view('view_home'); 
      }else{ 
       redirect(base_url('index.php/profile'),'refresh'); 
     } 
    } 

    function basisdata_cek($password){ 
     $email = $this->input->post('email'); 
     $result = $this->login->login($email,$password); 
     if($result){ 
      $sess_array = array(); 
      foreach($result as $row){ 
       $sess_array = $arrayName = array('email'=>$row->email, 'password'=>$row->password); 
       $this->session->set_userdata('logged_in',$sess_array); 
      } 
      return true; 
     }else{ 

      $this->session->set_flashdata('basisdata_cek', 'Invalid email or password'); 
      redirect(base_url('index.php/login'),'refresh'); 
      return false; 
     } 
    } 


} 
+1

什麼是$這個 - >值session-> userdata('logged_in')? –

+0

@AdhanTimothyYounes有我在編輯的代碼,我把值$這 - >會話級>用戶數據(「LOGGED_IN」) – Firm

+0

可能也許你正在破壞在登錄類第一手會議.... –

回答

0

BEST PRACT冰

總是創建用戶/登錄控制器單獨,你將有更多的空間來創建功能,如用戶角色檢查和重定向到他們的角色特定儀表板/配置文件。

請登錄/註銷校驗功能在父控制器和從控制器擴展控制器。例如,創建一個名爲My_Controller的控制器,並在其中放置您的isLoggedin檢查和註銷功能。

class My_Controller extends CI_Controller { 
    public function __construct() 
    { 
     parent::__construct(); 
    } 
    public function isLoggedIn() 
    { 
     if(!empty($this->session->userdata['id'])&& $this->session->userdata['type']=='admin') 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
    public function logout() 
    { 
     $this->session->sess_destroy(); 
     redirect(base_url()); 
    } 
} 

然後創建用戶或登錄控制器來呈現登錄頁面並執行登錄功能

class Login extends My_Controller 
{ 
public function __construct() 
{ 
    parent::__construct(); 
    $this->load->model('user_model'); 
    $this->load->library("security"); 
} 

public function index() 
{ 
    if(!$this->isLoggedin()) // if the user is not logged in render login screen 
    { 
    if($_POST) // or if($this->input->post) 
    { 
    $data=$this->security->xss_clean($_POST); 
    $user=$this->user_model->checkUser($data); 
    if(!empty($user)) 
    { 
     $this->session->set_userdata($user); 
     redirect(base_url().'profile'); 
    } 
    else 
    { 
     $data['errors']='Wrong Credentials'; 
     $this->load->view('login',$data); 
    } 
    } 
    else 
    { 
    $this->load->view('login'); 
    } 
    } 
    else // but if the user is logged in , take him to profile. 
    { 
    redirect(base_url().'profile'); 
    } 
} 

和簡介控制器

class Profile extends My_Controller 
{ 
public function __construct() 
{ 
    parent::__construct(); 
    $this->load->model('user_model'); 
} 

public function index() 
{ 
    // if user is not logged in , redirect him back to login screen. 
    if(!$this->isLoggedin()){ redirect(base_url().'login');} 
    $userId=$this->session->userdata['id']; // this index depends on the field name 
    $data['user']=$this->user_model->getUserDataById($userId); 
    $this->load->view('profile',$data); 
} 
} 
相關問題