尝试使用数据库验证用户信息时,Codeigniter功能不起作用

时间:2016-05-28 20:56:46

标签: codeigniter validation model

我正在创建一个登录系统,由于某种原因,即使我输入了正确的电子邮件和密码,它也会重定向回登录页面。被调用的模型函数似乎不起作用。这是我的代码:

控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
    class Login extends CI_Controller {



        function validation()
        {

            $this->load->library('form_validation');

            $this->form_validation->set_rules('email', 'Email', 'required|trim');
             $this->form_validation->set_rules('password', 'Password', 'required|md5');



            if ($this->form_validation->run()){

                redirect('login/valid_credentials'); 

            }else{

              $this->load->view('login_form');
              //return false;    
            }

        }




        function valid_credentials(){

           $this->load->model('login_model');
            // oh i see

             if ($this->login_model->match_login()){
                //return true;
                 $data = array(
                    'id'=>$q['id'], 

                    'email'=>$this->input->post('email'),
                  'is_logged_in'=> 1);
                $this->session->set_userdata($data);
                $this->load->view('dashboard_view', $data);

           }else{
                 $this->load->view('login_form');


            }

       }

型号:

class Login_model extends CI_Model{

    public function match_login(){

        $this->db->where('email', $this->input->post('email'));
        $this->db->where('password', md5($this->input->post('password')));
        $q = $this->db->get('user');

        if($q->num_rows()== 1){

          return true;

        }
    }
}

查看:

<?php

echo "<h1 class ='col-lg-10 col-lg-offset-5'>SIGN IN</h1>";


echo form_open('login/validation', $grid);


echo "<h2>Client</h2>";

"<h3>Please Login</h3>";

echo validation_errors();

echo "<p> Email: </br>";
echo form_input ('email');
echo "</p>";


echo "<p> Password: </br>";
echo form_password ('password');
echo "</p>";




echo "<p>";
echo form_submit ('submit', 'Signin');
 echo "</p>";

echo form_close();

?>

出于某种原因,它不起作用,我不确定我错过了什么。

1 个答案:

答案 0 :(得分:0)

无需在一个功能中验证表单在另一个功能中检查登录。使用一个功能来做所有事情。

<强>更改

  1. 控制器代码更改和优化
  2. 型号代码已更改
  3. 部分参考资料

    1. CodeIgniter Form Validation in FromGet.com(亲自推荐此网站)

    2. Form Validation in Codeigniter.com

    3. 试试这个

      <强>控制器

      <?php
      defined('BASEPATH') OR exit('No direct script access allowed');
          class Login extends CI_Controller {     
      
              function validation()
              {
      
                  $this->load->library('form_validation');
      
                  $this->form_validation->set_rules('email', 'Email', 'required|trim');
                  $this->form_validation->set_rules('password', 'Password', 'required|md5');
      
      
                  if ($this->form_validation->run()  == FALSE )
                  {
                      $this->load->view('login_form');
                      //return false; 
                  }
                  else{
      
                      $this->load->model('login_model');
      
                      $email = $this->input->post('email');
                      $password = $this->input->post('password');
      
                      $result = $this->login_model->match_login($email, $password);
      
                      if ($result ==false) {
                          echo "Invalid Cardinals";
                       } 
                       else 
                       {
                          $session = array(
                            'id'=>$result[0]['id'], 
                            'email'=>$this->input->post('email'),
                            'is_logged_in'=> 1
                          );
                          if (!$this->session->set_userdata($session)) {
      
                              $data['modelData'] = $result;
                              $data['sessionData'] = $session;
      
                              $this->load->view('dashboard_view', $data);
                          }
                           else {
                              echo "Error in session";.
                          }                          
                      }   
                  }            
              }
      

      <强>模型

      class Login_model extends CI_Model{
      
          public function match_login($email, $password){
      
              $this->db->where('email', $email);
              $this->db->where('password', md5($password));
              $query = $this->db->get('user');
      
              $result = $query->result_array();
      
              if (empty($result)) 
              {
                  return false;
              } 
              else 
              {
                  return $result;
              }
      
          }
      }