0

我开发了一个简单的登录系统,它工作正常但失败了,我需要知道为什么。

问题:如何显示导致失败的原因?

这是数据库功能:

function login($email,$password)
{
    $this->db->where("email",$email);
    $this->db->where("password",$password);

    $query=$this->db->get("users");
    if($query->num_rows()>0)
    {
        foreach($query->result() as $rows)
        {
            //add all data to session
            $newdata = array(
                'user_id'  => $rows->id,
                'user_name'  => $rows->username,
                'user_email'    => $rows->email,
                'logged_in'  => TRUE,
            );
        }
        $this->session->set_userdata($newdata);
        return true;
    }
    return false;
}

这是逻辑:

public function login()
{
    $this->load->library('form_validation');
    // field name, error message, validation rules
    $this->form_validation->set_rules('email', 'Your Email', 'trim|required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');

    if($this->form_validation->run() == FALSE)
    {
        $this->signin();
    }
    else
    {
        $email=$this->input->post('email');
        $password=md5($this->input->post('pass'));
        $result=$this->user_model->login($email,$password);
        if($result)
        {
            $this->dash();
        }
        else
        {
            $data['title']= 'Login Error';
            $this->load->view('nav/header', $data);
            $this->load->view('login', $data);
            $this->load->view('nav/footer', $data);
        }
    }
}

我知道错误正在发生,因为如果失败,我会重定向回登录页面并更改标题文本以显示给我(目前仅在测试模式下)。但是我怎样才能知道出了什么问题呢?

这是检查数据库功能:

function login($email,$password)
{
    $this->db->where("email",$email);
    $this->db->where("password",$password);

    $query=$this->db->get("users");
    if($query->num_rows()>0)
    {
        foreach($query->result() as $rows)
        {
            //add all data to session
            $newdata = array(
                'user_id'  => $rows->id,
                'user_name'  => $rows->username,
                'user_email'    => $rows->email,
                'logged_in'  => TRUE,
            );
        }
        $this->session->set_userdata($newdata);
        return true;
    }
    return false;
}
4

3 回答 3

0

我假设您所有的 php 代码都很好,那么您需要为每个输入设置自定义表单验证消息,以了解哪个输入出错并回显它们:

<?php echo validation_errors(); ?>
于 2013-05-10T08:34:57.870 回答
0

在您的视图文件中写入以下代码

<section id="notification" >
<?php
      if(validation_errors() !== '' ) { 
         echo "<div class='alert-msg error'>";
         echo validation_errors();
         echo "</div>";
      }
      $error = $this->session->flashdata('error');
      $success = $this->session->flashdata('success');
      if($error)
      {
        echo "<div class='alert-msg error'>";
        echo $this->session->flashdata('error');
        echo "</div>";
      }

      if($success)
      {
        echo "<div class='alert-msg success'>";
        echo $this->session->flashdata('success');
        echo "</div>";
      }
  ?>
</section>

并在控制器的闪存数据中有条件地设置成功/错误消息(见下文)

 if($result) {
    $this->dash();
    $this->session->set_flashdata('success', 'Login successfully.');
 } else {
     $this->session->set_flashdata('error', 'Login failed');
 }

阅读更多CI 中的 Flashdata

于 2013-05-10T08:30:38.803 回答
0

对于您更改的答案:

在您的模型中使用以下逻辑

 $qry = $this->db->get_where('users', array('username' => $this->_username ));
 if ($qry->num_rows() == 1) {           
    $user = $qry->row_array();
    $submitted_pass = md5($this->_password);
    $db_pass = $user['password'];
    if ($submitted_pass === $db_pass) {
        return $user;
    } else {
        // wrong username/password
        $this->session->set_flashdata('error', $this->errorList[10]);
        return FALSE;
    }
} else {
    // no such username exist     
    $this->session->set_flashdata('error', $this->errorList[15]);
    return FALSE;
}
于 2013-05-10T08:59:40.513 回答