CI - 告诉我出了什么问题

时间:2013-05-10 08:25:15

标签: php mysql codeigniter codeigniter-2

我开发了一个简单的登录系统,它运行正常但失败了,我需要知道原因。

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

这是数据库功能:

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

3 个答案:

答案 0 :(得分: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');
 }

了解更多Flashdata in CI

答案 1 :(得分:0)

我假设您的所有PHP代码都没问题,那么您需要为每个输入设置自定义form-validation-message以了解哪个输入出错并回显它们:

<?php echo validation_errors(); ?>

答案 2 :(得分: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;
}