2013-03-24 99 views
1

我試圖限制用戶未登錄的頁面訪問。奇怪的是,當試圖訪問未經授權的用戶時,網頁顯示頁面對於受限訪問(使用「未經授權,請登錄」的消息,並在同一頁面上,它只加載成員頁面)。Codeigniter會話認證仍允許加載受限制的頁面

對於現場控制器:

class Site extends CI_Controller { 

function __construct(){ 

    parent::__construct(); 

    $isLogged=$this->session->userdata('logged'); 
    if($isLogged!='logged' || !isset($isLogged)){ 

     $data['content']='denied'; 
     $this->load->view('include/template', $data); 

    } 


} 

function members(){ 

     $data['content']='memberArea'; 
     $this->load->view('include/template', $data); 

} 

登錄表單:

class Login extends CI_Controller { 


function index() { 

    $data['content']='loginForm'; 
    $this->load->view('include/template', $data); 
} 


function validateMember(){ 

    // load the model for the login authentification 
    $this->load->model('loginModel'); 
    //query should also contain the user details 
    $query = $this->loginModel->authenticate(); 

    //if it returns something: 
    if($query){ 

     foreach ($query as $row){ 

      //set user data to be passed  
      $details=array(
      'username'=>$row->user_name, 
      'id'=>$row->id, 
      'logged'=>'logged',); 

     } 

    //set session 
    $this->session->set_userdata($details); 

    redirect('site/members'); 

    } 

    else{ 

     $this->index(); 
    } 


} 

的登錄的模式是:

class LoginModel extends CI_Model { 

function authenticate(){ 

//select active fields 
$this->db->select('id, user_name, password');  
// query to select from table where conditions 
$query = $this->db->get_where('login', array(
      'user_name'=>$this->input->post('username'), 
      'password'=>$this->input->post('password'),), 
        1); 

    //if it finds something...must ask if i need to manually close the database with $this->db->close(); 
    if($query->num_rows()==1){ 

     foreach($query->result() as $row){ 

      $data[]=$row; 
     } 

     return $data; 
    } 

    else {return false;} 
} 

}

我的測試表明,現場繼續調查l即使構造函數失敗,其他函數也是如此。會話確實包含數據。如果我使用die()或exit(),則網頁加載空白。提前謝謝了!

PS:只有<p>在他們的意見,沒有什麼幻想。

+0

嗨,如果你想添加自己的答案,請做 - 但在答案框中這樣做,而不是作爲問題的更新。不要在標題中添加「已解決」,請勾選您最喜歡的答案(如果您願意,也可以選擇自己的答案)。我已經回到以前的版本,但你可以[從這裏複製你的編輯](http://stackoverflow.com/posts/15599079/revisions)。 – halfer 2013-03-24 14:27:45

+0

哦,對不起。我會選擇我最喜歡的答案。沒有進攻意圖的球員。 – 2013-03-24 14:56:40

回答

1

我可以看到這個問題的兩個解決方案。

  1. 重定向到另一個不檢查身份驗證的頁面。您可以使用URL助手中的redirect(<url>);

  2. 使用exit()__construct()與緩衝輸出刷新。當您撥打$this->load->view()時,數據將發送到CodeIgniter中名爲output的緩衝區。您可以通過執行寫緩衝區:

    if($isLogged!='logged' || !isset($isLogged)){ 
    
        $data['content']='denied'; 
        $this->load->view('include/template', $data); 
    
        // Write the output. 
        echo $this->output->get_output(); 
    
        // Stop the execution of the script. 
        exit(); 
    } 
    

    ,或者你可以繞過輸出緩衝器:曾經你想要的

    if($isLogged!='logged' || !isset($isLogged)){ 
    
        $data['content']='denied';   
    
        // Writes the content instead of sending it to the buffer. 
        echo $this->load->view('include/template', $data, true); 
    
        // Stop the execution of the script. 
        exit(); 
    } 
    

挑選。

0

構造函數方法沒有「失敗」,if語句被簡單地執行(因爲條件爲真),並且沒有理由阻止其他方法被執行。

在構造函數方法中,在if塊的末尾,可以插入exit語句,這樣頁面可能會按照您的預期行爲。

看到這裏http://php.net/manual/en/function.exit.php

+0

好吧,if語句被執行,因爲它加載了受限制的訪問頁面內容。它只是繼續加載其他頁面以及... – 2013-03-24 13:51:31

+0

對不起,你是對的,但問題仍然是,你沒有阻止其他方法與構造函數一起執行,你需要退出語句,現在我編輯我的回答。 – mastazi 2013-03-24 13:55:35

+0

請參閱已編輯的答案。 – mastazi 2013-03-24 13:59:15

0

您正在測試,如果有一個查詢,如果沒有查詢實際上返回任何東西。檢查num行並確保它們等於1,大於0是在登錄檢查時的不良做法。

if($query->num_rows==1){ 

PS。發佈你的模型代碼也是可能的,這個錯誤在那裏,即使它不應該返回結果。

+0

謝謝你的建議,但是在將數據傳遞給控制器​​之前,在模型中檢查了num_rows。我無法在控制器中檢查它,因爲它表示非對象。 – 2013-03-24 14:05:08