2017-10-15 106 views
0

我已經作出了註冊和登錄應用與笨3.笨用戶註冊:分流登錄失敗在2個條件的條件當有人填充<em>登記表</em>並將其提交到覆蓋「帳戶無效」情況下

成功時,「用戶」表中的「活動」列中接收值0,作爲可見圖像中的波紋管:

enter image description here

用戶將能夠前激活他們的帳戶小號。IGN在

在Signin.php控制器我有signin()功能:

public function signin() 
{ 
    $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email'); 
    $this->form_validation->set_rules('password', 'Password', 'required|trim'); 
    $this->form_validation->set_error_delimiters('<p class="error">', '</p>'); 
    if ($this->form_validation->run()) 
    { 
    $email = $this->input->post('email'); 
    $password = $this->input->post('password'); 
    $this->load->model('Usermodel'); 
    $current_user = $this->Usermodel->user_login($email, $password, $active); 
    // Set the current user's data 
    if ($current_user) { 
    $this->session->set_userdata(
     array(
     'user_id' => $current_user->id, 
     'user_email' => $current_user->email, 
     'user_first_name' => $current_user->fname, 
     'is_logged_in' => TRUE 
     ) 
     ); 
    redirect('home'); 
    } else { 
     $this->session->set_flashdata("signin_failure", "Incorrect email or password"); 
     redirect('signin'); 
    } 
    } 
    else 
    { 
    $this->load->view('signin'); 
} 
} 

我想要的,而不是在代碼行$this->session->set_flashdata("signin_failure", "Incorrect email or password");以上,以便能夠「分裂」登錄失敗條件在2:不正確的電子郵件或密碼帳戶未被激活

if (condition here) { 
     $this->session->set_flashdata("signin_failure", "Your account has not been activated"); 
    } else { 
     $this->session->set_flashdata("signin_failure", "Incorrect email or password"); 
} 

問題:我應該怎麼放,而不是condition here在上面的代碼?

更具體地說:我怎麼說的:如果「活動」列中的值是0做$this->session->set_flashdata("signin_failure", "Your account has not been activated");

的USER_LOGIN()內部函數的的usermodel

public function user_login($email, $password, $active) { 
     $query = $this->db->get_where('users', ['email' => $email, 'password' => md5($password), 'active' => 1]); 
     return $query->row(); 
} 

UPDATE:

我想出了這一點:

public function signin() 
    { 
    $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email'); 
    $this->form_validation->set_rules('password', 'Password', 'required|trim'); 
    $this->form_validation->set_error_delimiters('<p class="error">', '</p>'); 
    if ($this->form_validation->run()) 
    { 
    $email = $this->input->post('email'); 
    $password = $this->input->post('password'); 
    $this->load->model('Usermodel'); 
    $current_user = $this->Usermodel->user_login($email, $password); 
     // If we find a user 
    if ($current_user) { 
     // If the user found is active 
     if ($current_user->active == 1) { 
     $this->session->set_userdata(
     array(
      'user_id' => $current_user->id, 
      'user_email' => $current_user->email, 
      'user_first_name' => $current_user->fname, 
      'user_active' => $current_user->active, 
      'is_logged_in' => TRUE 
     ) 
     ); 
     redirect('home'); 
     } else { 
     // If the user found is NOT active 
     $this->session->set_flashdata("signin_failure", "Your account has not been activated"); 
     redirect('signin'); 
     } 
    } else { 
     // If we do NOT find a user 
     $this->session->set_flashdata("signin_failure", "Incorrect email or password"); 
     redirect('signin'); 
    } 
    } 
    else 
    { 
    $this->load->view('signin'); 
} 
} 

但在它的一個缺陷,因爲即使當電子郵件地址和密碼是否正確,但用戶不活躍,該消息是:「不正確的電子郵件或密碼」而不是「您的帳戶尚未激活」。

+0

將if($ current_user-> active == 1)'改爲'if($ current_user ['active'] == 1)'因爲函數返回的結果是數組而非對象。 –

+0

我剛剛做了,它給出了這個錯誤:'不能使用stdClass類型的對象作爲數組'。 –

+0

使用'return $ query-> row_array();'在模型函數 –

回答

1

剛剛從user_login函數模型刪除主動檢查。因爲你已經檢查ID的用戶是主動或不在您的控制器。它不應該影響你的工作。

$query = $this->db->get_where('users', ['email' => $email, 'password' => md5($password)]); 

編輯:

那麼在笨論壇闡述了答案貢獻JayAdrahere

這是因爲你的第一個if語句是:

if ($current_user) { 

哪樣爲非活動用戶返回false,作爲你的que ry是:

$query = $this->db->get_where('users', ['email' => $email, 'password' => md5($password), 'active' => 1]); 

注意,檢查「active」=> 1,這意味着它不會爲非活動用戶返回任何記錄。

所以,你的第一個if語句返回false,因此將其擁有的其他條款:

$this->session->set_flashdata("signin_failure", "Incorrect email or password"); 

所以你可能需要檢查,如果用戶是第一次激活時,檢查前如果他們的用戶名/密碼是正確的。

我建議將你的「user_login」功能分成兩個不同的功能。一個用於檢查用戶是否處於活動狀態,另一個用於測試用戶/傳遞組合。

最後,我注意到你將密碼存儲爲md5字符串......這是一個壞主意。這並不安全。使用bcrypt或類似的。

0
/***************************************/ // model function 
function user_login($email,$password) 
{ 
    $this->db->select("*"); 
    $this->db->from('table_name'); 
    $this->db->where(array('email'=>$email,'password'=>$password)); 
    $this->db->limit(1); 
    $query = $this->db->get(); 
    if(!$query->num_rows()) 
     return false; 
    return $query->row_array(); 
} 
/***************************************/ // controller 
public function signin() 
{ 
    $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email'); 
    $this->form_validation->set_rules('password', 'Password', 'required|trim'); 
    $this->form_validation->set_error_delimiters('<p class="error">', '</p>'); 
    if ($this->form_validation->run()){ 
     $email = $this->input->post('email'); 
     $password = $this->input->post('password'); 
     $this->load->model('Usermodel'); 
     $current_user = $this->Usermodel->user_login($email, $password); 
     // If we find a user 
     if ($current_user) { 
      // If the user found is active 
      if ($current_user['active'] == 1) { 
       $this->session->set_userdata(array(
        'user_id' => $current_user['id'], 
        'user_email' => $current_user['email'], 
        'user_first_name' => $current_user['fname'], 
        'user_active' => $current_user['active'], 
        'is_logged_in' => TRUE 
       )); 
       redirect('home'); 
      }else { 
       // If the user found is NOT active 
       $this->session->set_flashdata("signin_failure", "Your account has not been activated"); 
       redirect('signin'); 
      } 
     }else { 
      // If we do NOT find a user 
      $this->session->set_flashdata("signin_failure", "Incorrect email or password"); 
      redirect('signin'); 
     } 
    } 
    else{ 
     $this->load->view('signin'); 
    } 
} 
+0

請將您的控制器代碼放在_my_ Signin控制器代碼的上下文中。我有很多代碼,更改變量名稱會產生很多錯誤。謝謝! –

+0

好吧,我編輯過您的舒適 –