dongzi0850 2017-06-15 07:58
浏览 32
已采纳

用户验证在CodeIgniter中不起作用

I am new in codeigniter. I create an login form and i want to validate my login credentials. I search many solutions but i failed. I cannot validate my credentials.

Here is My view Code name as login_form.php

<div id="login_form">
        <h1>Login Page..!!</h1>
        <?php
        echo form_open('login/validate_credentials');
        echo form_input('username', 'Username');
        echo form_password('password', 'Password');
        echo form_submit ('submit', 'Login');
        ?>

    </div>

Here is My Model Code name as users_model.php

<?php
class Users_model extends CI_Model{

        function validate(){

                $this->db->where('username', $this->input->post('username'));
                $this->db->where('password', $this->input->post('password'));
                $query = $this->db->get('users');
if ($query->num_rows == 1){
                    return true;
                    }

            }

    } ?>

Here is My Controler Code name as Login.php

<?php

    class Login extends CI_Controller{

        function index(){
            $this->load->helper('url'); 
            $this->load->helper('html');
            $data['main_content'] = 'login_form';
            $this->load->view('include/template', $data);
        }

        function validate_credentials(){

            $this->load->model('users_model');
            $query = $this->users_model->validate();




            if($query){


                $data = array(
                                'username'=> $this->input->post('username'),
                                'is_logged_in' => true
                );

                    $this->session->set_userdata($data);
                    redirect('site/dashboard');

            }

            else{
                echo "Wrong";
                //$this->index();   
            }
        }
    }
?>

Even when i give it right username and password it cannot validate and echo "Wrong" as shown in my code

  • 写回答

1条回答 默认 最新

  • dongmaonao0505 2017-06-15 08:05
    关注

    num_rows() should be a method

    change to it:

      function validate()
      {
    
                $this->db->where('username', $this->input->post('username'));
                $this->db->where('password', $this->input->post('password'));
                $query = $this->db->get('users');
                if ($query->num_rows() == 1){
                    return true;
                    }
    
      }
    

    https://www.codeigniter.com/user_guide/database/results.html#id4

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?