666 发表于 2013-9-6 08:28:48

求助一个登陆验证问题

本帖最后由 666 于 2013-9-6 09:20 编辑

打算用CI写一个简单的博客,可是卡在登陆验证这里了,请各位帮助看一下:

问题是:随便输入用户名和密码都可以进入后台

主要代码如下:

Controller: Admin


<?php
class Admin extends CI_Controller{

      public function __construct()
      {
                parent::__construct();
                // $this->load->model('Blog_model');
                $this->load->model('admin_model');
                $this->is_logged_in();
      }

      function index()
      {
                $this->load->view('admin/dashbaord');
      }
}


Controller: Login


<?php
class Login extends CI_Controller{

      function index(){
                $this->load->view('admin/login_form');
      }

      function validate_credentials()
      {
                $this->load->model('admin_model');
                $query = $this->admin_model->validate();

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

                              $this->session->set_userdata($data);
                              redirect('admin','refresh');
                }
                else
                {
                        $this->index();
                }
      }

}


Model: admin_model


<?php
class Admin_model extends CI_Model {

      function __construct()
    {
      parent::__construct();
    }

      function validate()
      {
                $this->db->where('name',$this->input->post('username'));
                $this->db->where('password',MD5($this->input->post('password')));
                $query = $this->db->get('admin');

                if($query->num_rows = 1)   // 自己琢磨着解决了 此处修改为if($query->num_rows() > 0)
                {
                        return true;                  // 此处修改为$query = true;
                                                            //                     return $query;
                }
      }

}


View: admin/login_form


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charest=utf-8">
<title>Login</title>
<link rel="stylesheet" href="<?php echo base_url();?>css/login-style.css" type="text/css" media="screen" title="no title" charest="utf-8">
</head>
<body>

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

</body>
</html>

666 发表于 2013-9-6 08:47:07

@Hex 老大 帮我看看吧

kinwyb 发表于 2013-9-6 09:20:10

admin_model里的
if($query->num_rows = 1)
                {
                        return true;
                }
这句话判断是错误的
1.获取结果数应该是$query->num_rows();
2.对比应该是==不是=,用=号的话是永远成立的,所以你用什么帐号都会通过验证

666 发表于 2013-9-6 09:21:41

kinwyb 发表于 2013-9-6 09:20 static/image/common/back.gif
admin_model里的
if($query->num_rows = 1)
                {


谢谢指点

dren_a 发表于 2013-9-6 13:17:20

用这个:
$query->num_rows
CI会报错吧。。。

666 发表于 2013-9-6 13:29:47

本帖最后由 666 于 2013-9-6 13:32 编辑

dren_a 发表于 2013-9-6 13:17 static/image/common/back.gif
用这个:

CI会报错吧。。。

if($query->num_rows = 1)


一开始这个还真没报错。。。但是会全部通过验证


if($query->num_rows() == 1)


这样就解决了

dren_a 发表于 2013-9-6 13:53:50

666 发表于 2013-9-6 13:29 static/image/common/back.gif
一开始这个还真没报错。。。但是会全部通过验证




我当初忘了写括号他就报错了。。。

一路绝尘 发表于 2014-1-27 12:23:43

本帖最后由 一路绝尘 于 2014-1-27 12:28 编辑

kinwyb 发表于 2013-9-6 09:20 static/image/common/back.gif
admin_model里的
if($query->num_rows = 1)
                {

求教一下,$query->num_rows == 1 为什么能代表密码和用户名一样了?

哦我明白怎么回事了。。。。晕了
页: [1]
查看完整版本: 求助一个登陆验证问题