|
本帖最后由 666 于 2013-9-6 09:20 编辑
打算用CI写一个简单的博客,可是卡在登陆验证这里了,请各位帮助看一下:
问题是:随便输入用户名和密码都可以进入后台
主要代码如下:
Controller: Admin
PHP复制代码
<?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复制代码
<?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复制代码
<?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复制代码
<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>
复制代码
|
|