2017-09-22 7 views
0

電子メールアドレスでログインすることができません。ログインはユーザー名のみで行います。メールでログインしようとすると、ログインページにリダイレクトされます。 私も助けてください。私も電子メールでログインできます。 多くのオンラインソースを確認しましたが、私にとっては役に立たないです。codeigniterでユーザ名またはメールアドレスを使用してログインします。

マイコントローラ:

STORE_ACCOUNTS_CONTROLLER 関数get_with_double_condition($のCOL1、$値1、$のCOL2、$値2)

{ 
    $this->load->model('mdl_store_accounts'); 
    $query = $this->mdl_store_accounts->get_with_double_condition($col1, $value1, $col2, $value2); 
    return $query; 
} 

YOURACCOUNTS_CONTROLLER 関数submit_login()

{ 
     $this->load->module('site_security'); 
     $this->load->module('store_accounts'); 
     $submit = $this->input->post('submit',TRUE); 
     if ($submit=="Submit") { 
     //process the form 
     $this->load->library('form_validation'); 
     $this->form_validation->set_rules('username','Username','required|min_length[5]|max_length[60]|callback_username_check'); 
     $this->form_validation->set_rules('pword','Password','required|min_length[7]|max_length[35]'); 
     if ($this->form_validation->run()==TRUE) { 
      //figure out user_id 
      $col1 = 'username'; 
      $value1 = $this->input->post('username', TRUE); 
      $col2 = 'email'; 
      $value2 = $this->input->post('email', TRUE); 
      $query = $this->store_accounts->get_with_double_condition($col1, $value1, $col2, $value2); 
      foreach ($query->result() as $row) { 
       $user_id = $row->id; 
      } 
      //send them to the private page 
      $this->_in_you_go($user_id, $login_type); 

      $remember = $this->input->post('remember', TRUE); 
      if ($remember=="remember-me") { 
       $login_type = "longterm"; 
      } 
      else 
      { 
       $login_type = "shortterm"; 
      } 


     } 
     else 
     { 
      echo validation_errors(); 
     } 
    } 
} 

関数username_check($ strの)

{ 
    $this->load->module('site_security'); 
    $this->load->module('store_accounts'); 
    $error_msg = "You did not enter a correct username and/or password."; 

    $col1 = 'username'; 
    $value1 = $str; 
    $col2 = 'email'; 
    $value2 = $str; 
    $query = $this->store_accounts->get_with_double_condition($col1, $value1, $col2, $value2); 
    $num_rows = $query->num_rows(); 

    if ($num_rows<1) { 
     $this->form_validation->set_message('username_check', $error_msg); 
     return FALSE; 
    } 

    foreach ($query->result() as $row) { 
     $pword_on_table = $row->pword; 
    } 
    $pword = $this->input->post('pword', TRUE); 
    $result = $this->site_security->_verify_hash($pword, $pword_on_table); 

    if ($result==TRUE) { 
     return TRUE; 
    } 
    else 
    { 
     $this->form_validation->set_message('username_check', $error_msg); 
     return FALSE; 
    } 

} 

マイモデル:

STORE_ACCOUNTS_MODEL

関数get_with_double_condition($のCOL1、$値1、$のCOL2、$値2)

{ 
    $table = $this->get_table(); 
    $this->db->where($col1, $value1); 
    $this->db->or_where($col2, $value2); 
    $query=$this->db->get($table); 
    return $query; 
} 

マイビュー:

LOGIN_PAGE

<div class="container"> 
    <div class="row"> 
    <div class="col-md-4 col-md-offset-4"> 
     <form class="form-signin" action="<?= $form_location ?>" method="post"> 
     <h2 class="form-signin-heading">Please sign in</h2> 
     <label for="inputText" class="sr-only">Username or Email address</label> 
      <input type="text" name="username" value="<?= $username?>" id="inputEmail" class="form-control" placeholder="Username or Email address" required autofocus> 
     <label for="inputPassword" class="sr-only">Password</label> 
      <input type="password" name="pword" id="inputPassword" class="form-control" placeholder="Password" required> 
     <div class="checkbox"> 
     <label> 
      <input type="checkbox" name="remember" value="remember-me"> Remember me 
     </label> 
     </div> 
     <button name="submit" value="Submit" class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> 
     </form> 
     </div> 
     </div> 

    </div> <!-- /container --> 

答えて

1

フォームは唯一のPOSTのユーザー名です。

電子メールの場合は、このコードでは検証しません

$this->form_validation->set_rules('username','Username','required|min_length[5]|max_length[60]|callback_username_check'); 

と$値2

$value2 = $this->input->post('email', TRUE); 
+0

私はユーザー名を投稿するだけでメールとユーザー名を送信しようとしています。なぜなら、私は二重条件モデルを与えたのです。 –

+0

これでいいですが、この '$ this-> input-> post( 'email'、TRUE);を使用することはできません。 –

+0

これは$ value1と$ value2の両方に使用してください。 '$ value2 = $ this-> input-> post( 'username'、TRUE);' –

0

空になります私は病気の全体のコードを読んでその意志簡単なコードを提供しdidntは効果的である:

表示ファイル:

<form action='controller.php' method='POST'> 
<input type='email' name='email'> 
<input type='password' name='pass'> 
<input type='submit' value='login' name='login'> 
</form> 

Controller.php

((ステートメントの$ this - > login_model->ログインを実行するために、最初のコンストラクタでモデルを定義する);)

if(isset($_POST['login'])){ 
    $condition=$this->login_model->login(); 
    if(!empty($condition) && isset($condition)){ 
    redirect('');//redirect to where ever you want or you can set the session as well 
}} 

login_model.php

public function login(){ 
$email=$_POST['email']; 
$password=$_POST['pass']; 

$this->db->select('*'); 
$this->db->from('table_name'); 
$this->db->where('email',$email); 
$this->db->where('pass',$password); 
$q= $this->db->get()-results(); 
return $q; 
} 
+0

私のコードは正常に動作していますが、電子メールのログインに失敗します。 –

関連する問題