2017-10-15 11 views
0

「非アクティブアカウント」カバーするために2つの条件に条件を失敗します成功し、「ユーザー」テーブルの「アクティブ」の欄には、怒鳴る画像に見えるように、値0を受け取りますのCodeIgniterユーザ登録:スプリットログインが誰かが<em>登録フォーム</em>を記入し、それを送信すると、私はCodeIgniterの3</p> <p>で<strong>登録とログイン</strong>アプリケーションを作った場合

enter image description here

ユーザーがsにできるようになる前に、自分のアカウントを有効にする必要があります「スプリット」ログイン失敗の条件をできるようにするには、代わりに上記のコードの行$this->session->set_flashdata("signin_failure", "Incorrect email or password");で、

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'); 
} 
} 

私が欲しい:。私はsignin()機能を持っているSignin.phpコントローラで

でIGN 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の場合do $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 [ 'アクティブ'] == 1)に変更' IF($ current_user->アクティブ== 1) '配列オブジェクトではないとしての機能が結果を返すため。 –

+0

私はやりましたが、このエラーには '' stdClass型のオブジェクトを配列として使用できません。 –

+0

モデル関数の 'return $ query-> row_array();'を使用してください –

答えて

1

user_login機能のチェックをモデルから削除するだけです。あなたがすでにidをチェックしているので、ユーザはあなたのコントローラ内でアクティブであるかどうか。それはあなたの仕事に影響を与えるべきではありません。

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

EDIT:

まあ、あなたの最初のため、ステートメントがある場合、これはCodeIgniterのフォーラムにJayAdraによってhere

を回答貢献を精緻化:

意志
if ($current_user) { 

非アクティブなユーザの場合、あなたのキューとしてfalseを返します。 ryは:

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

「アクティブ」=> 1であることを確認すると、非アクティブユーザーのレコードは返されません。

だからあなたの最初の文が故に持つelse節に行く、falseを返す場合:

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

だから、あなたはおそらく、ユーザーが自分のユーザー名/パスワードが正しいかどうかをチェックする前に、最初にアクティブになっているかどうかを確認する必要があります。

私は2つの異なる機能にあなたの「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

ok私はあなたの快適さのために編集しました –

関連する問題