2016-12-16 50 views
0

我遇到問題。我正在登錄頁面上設置會話。但是當頁面重定向時,會話在第一次嘗試時不會啓動。當我第二次登錄時,會話已設置。codeigniter會話未設置第一次

我被困住了是什麼問題。

這裏是我的代碼

$data = $this->input->post(); 
    //$data['password'] = $this->encrypt->encode($data['password']); 
    $result = $this->Login_m->get_userdata($data['username']); 
    $result['password'] = $this->encrypt->decode($result['password']); 
    //print_r($result); 

    if (strtolower($data['username']) === strtolower($result['username']) and $data['password'] === $result['password']) { 

     unset($result['password']); 
     $this->session->set_userdata('user', $result); 
     redirect("dashboard"); 
    }else{ 
     $this->session->set_flashdata("error","Given Username or Password is not correct"); 
     redirect("login"); 
     die(); 
    } 

請幫助。 預先感謝您。

+0

做'的var_dump($這 - >會話級>用戶數據(「用戶」);'只是'redirect'之前,看看如果會話設置正確 –

+1

嘗試重定向('dashboard','refresh');另外,我現在總是直接訪問會話變量$ _SESSION ['some_name']。就像旁邊一樣,加密編碼和解碼不應該是用於密碼,你只能使用單向散列函數,在php.net這裏有詳細描述http://php.net/manual/en/function.password-hash.php – PaulD

+0

非常感謝你的信息。 PaulD,我會遵循這一點。 –

回答

0

我認爲這是因爲瀏覽器緩存。嘗試在刷新設置下面沒有緩存頭的規則(或者在你的構造函數):

header('Cache-Control: no-cache'); 
header('Pragma: no-cache');