2012-11-13 37 views
2

首先,對不起我的英語:DSession empty codeigniter

我試圖做一個簡單的登錄Codeigniter沒有成功。我閱讀官方文檔並將腳本降至最低限度。每次我重定向o刷新我的頁面或如果我轉到另一個控制器時,我的會話都是空的。如果我只創建一個控制器並進行簡單的讀寫操作,直到我刷新或重定向。

這就是我想要做的: 1)第一個控制器是主控制器。使用登錄表單加載視圖。此表單對方法validation_form和username_check回調有一個操作。 2)如果用戶能夠登錄,我設置我的用戶數據和重定向到限制控制器,這是我的禁區。

P.S.我在自動加載中使用庫會話並激活選項數據庫。我也有自動加載的數據庫庫。

主控制器

class Main extends CI_Controller { 


public function __construct(){ 
    parent::__construct(); 

} 

public function index() 
{ 
    $this->load->view('login_view'); 
} 

public function validation_form() 
{ 
    $this->load->library('form_validation'); 
    $this->form_validation->set_rules('email', 'Email', 'required|callback_username_check'); 
    if ($this->form_validation->run() == FALSE){ 
     echo 'validation_error'; 
    }else{ 
     redirect('/restricted'); 
    } 
} 

public function username_check(){ 
    $this->load->model('login_model'); 
    $email=$this->input->post('email'); 
    $login=$this->login_model->validate($email); 
    if ($login != FALSE){      
       return true; 
    } 
    return false; 
} 

}

LOGIN MODEL

class Login_model extends CI_Model{ 

public function __construct(){ 
    parent::__construct(); 
} 

public function validate($email){ 

    // Prep the query 
    $this->db->where('EmailDatore', $email); 

    // Run the query 
    $query = $this->db->get('Datore'); 
    // Let's check if there are any results 
    if($query->num_rows() == 1) 
    { 
     // If there is a user, then create session data 
     $row = $query->row(); 
     $data = array(
       'email' => $row->EmailDatore, 
       'nome' => $row->Nome, 
       'cognome' => $row->Cognome,      
       'validated' => true 
       ); 
     $this->session->set_userdata($data); 
     return true; 
    } 
    // If the previous process did not validate 
    // then return false. 
    return false; 
} 

}

受限CONTROLLER 類限制性延伸是CI_Controller {

public function __construct(){ 
    parent::__construct(); 
    $this->check_isvalidated(); 
} 

public function index(){ 
    // If the user is validated, then this function will run 
    echo 'Congratulations, you are logged in.'; 
} 

private function check_isvalidated(){ 
    if(! $this->session->userdata('validated')){ 
     redirect('main'); 
    } 
} 

}

+0

你在哪裏調用你的控制器函數username_check? –

+0

$ this-> form_validation-> set_rules('email','Email','required | callback_username_check'); –

+0

JUST REMOVED session_start()from main controller的構造。這是一個測試:D –

回答

1

錯字在模型中,無法確定是否有其他錯誤,但至少有這一項,這意味着你永遠不會完成你的成功代碼。

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

編輯 - 錯誤在這裏也是,你傳遞的是$數據,但人口$電子郵件。因此該模型獲得一個空字符串。

public function username_check(){ 
$this->load->model('login_model'); 
$email=$this->input->post('email'); 
$login=$this->login_model->validate($email); 
if ($login != FALSE){      
      return true; 
} 
return false; 
} 
+0

謝謝!我改正了它,但仍然不起作用 –

+0

我改正了這一點: - $ login = $ this-> login_model-> validate($ data);在$ login = $ this-> login_model-> validate($ email); -add;在線$ this-> load-> view('login_view') –

+0

你沒有看到的是發送登錄數據的視圖。目前,它幾乎看起來像是完全跳過驗證,因爲表單驗證會直接進入受限制的頁面。從視圖到登錄到您去限制頁面的地方,事件的順序是什麼? –

1

我覺得回調函數也可以這樣。

public function username_check($str){ 
    $this->load->model('login_model'); 
    $login=$this->login_model->validate($str); 
    if ($login != FALSE){      
      return true; 
    } 
return false; 

是您的代碼到達模型抑或是失敗的回調?哪部分代碼沒有到達?放置vardump();或者死();去檢查。

+0

一切順利在這一部分。模型是load和validate()返回true。這個「如果($ this-> form_validation-> run()== FALSE)」是真的,我已經重定向到禁區,它很好。但在限制的控制器中,會話是空的,所以$ this-> check_isvalidated();重定向我登錄 –