2016-12-23 41 views
2

我已經使用驗證碼和使用回調規則來檢查和驗證驗證碼輸入。codeigniter會話不能在同一個控制器的其他方法上工作

所以,我試圖將captcha變量發送到使用會話的同一控制器中的另一個方法。

當我在另一個函數中回顯會話時,它不起作用。它只在我嘗試在同一個函數中回顯它時才起作用。

下面的代碼:

1.登錄功能

public function login() 
{ 
    if($this->auth->is_logged_in() === TRUE) 
    { 
     redirect(base_url()."dashboard"); 
    } 

    $username = $this->input->post('username'); 
    $password = $this->input->post('password'); 

    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean'); 
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean'); 
    $this->form_validation->set_rules('captcha', 'Captcha', 'required|callback_check_captcha'); 

    if($this->form_validation->run() == FALSE) 
    { 
     $cap    = $this->created_captcha(); 
     $data['cap_img'] = $cap['image']; 
     $data['title']  = 'Form Login Administrator'; 

     $this->session->set_userdata('captcha', $cap['word']); 
     $sess = $this->session->userdata('captcha'); 

     echo ' <br/> FIRST SESSION : '.$sess . ' --- '; 

     $this->load->view('form-login', $data); 

    } 
    else 
    { 
     $check_login = $this->auth->do_login($username,$password); 

     if($check_login == FALSE) 
     { 
      $this->session->set_flashdata('msg', 'Your username or password wrong, please repeat again !!!'); 
      redirect(base_url().'login'); 
     } 
     else 
     { 
      $lastlogin = $this->dbm->update('users', array('userid'=>$this->session->userdata('userid')), array('lastlogin'=>date('Y-m-d H:i:s'))); 

      $this->session->unset_userdata('captcha'); 

      redirect(base_url().'dashboard'); 

     } 
    } 
} 

2.檢查capthca功能

public function check_captcha($input) 
{ 
    echo '<br/> SECOND SESSION : '. $this->session->userdata('captcha') . ' --- ' . 
     '<br/> POST INPUT : '.$input.' --- '; 

    if($input === $this->session->userdata('captcha')) 
    { 

     return TRUE; 
    } 
    else 
    { 
     $this->form_validation->set_message('check_captcha', 'you input the wrong %s!'); 

     return FALSE; 
    } 
} 

我試圖把會話庫中構造函數,但仍然是一樣的。

function __construct() 
{ 
    parent::__construct(); 
    $this->load->library('session'); 
    $this->userid = $this->session->userdata('userid'); 
    $this->load->helper('captcha'); 
} 

我在這裏犯錯嗎?請幫助,謝謝。

編輯: 這隻發生在Chrome瀏覽器。

+0

前行代碼? –

+0

它加載到config目錄下的autoload.php中。 – Ikhsan

回答

0

您沒有在check_captcha方法中獲取驗證碼會話,因爲您的驗證碼會話僅在未驗證表單時創建。

認沽下面在這裏裝載`form_validation`庫if($this->form_validation->run() == FALSE)..

$cap    = $this->created_captcha(); 
    $data['cap_img'] = $cap['image']; 
    $data['title']  = 'Form Login Administrator'; 

    $this->session->set_userdata('captcha', $cap['word']); 
+0

它不起作用,這個會話問題只發生在我使用chrome時 – Ikhsan

相關問題