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 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误