使用codeigniter

时间:2017-09-22 08:17:40

标签: codeigniter email login username

我无法使用电子邮件地址登录。登录只通过用户名完成,当我尝试使用电子邮件登录时,它会将我重定向到登录页面。 请帮帮我,所以我也可以用电子邮件登录。 我检查了许多在线资源但对我没有帮助。

我的控制器:

STORE_ACCOUNTS_CONTROLLER function get_with_double_condition($ col1,$ value1,$ col2,$ value2)

{
    $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();
        }
    }
}

function 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

function get_with_double_condition($ col1,$ value1,$ col2,$ value2)

{
    $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 -->

2 个答案:

答案 0 :(得分:1)

您的表单只是POST用户名。

如果是电子邮件,请不要在代码中验证

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

和$ value2将为空

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

答案 1 :(得分:0)

我没有阅读整个代码,为您提供一个有效的简单代码:

查看文件:

<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-&gt; login_model-&gt; login();)

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