3 回答
TA贡献1797条经验 获得超4个赞
首先,我不知道$data在Login 控制器的index()中做什么,以及validate()在模型 admin中做什么,那么我认为最好放置登录和注销功能在登录控制器中如下:
<?php
class Login extends CI_Controller{
function __construct(){
parent::__construct();
}
function index() {
$this->load->view('admin/login');
}
public function log_in()
{
$this->load->model('admin');
$this->form_validation->set_rules('username', 'Username', 'trim|required', array('required' => 'Username required'));
$this->form_validation->set_rules('password', 'Password', 'trim|required', array('required' => 'Password required'));
//username:admin password:123456
if ($this->form_validation->run() !== false) {
$username = $this->input->post('username');
$password = $this->input->post('password');
$check = $this->admin->validate($username, $password);
if ($check != false) {
$this->session->set_userdata('admin','1');
redirect('Dashboard');
} else {
redirect('Login');
}
}
$this->load->view('admin/login');
}
public function log_out()
{
$this->session->sess_destroy();
redirect('Login');
}
}
?>
那么你的仪表板控制器如下:
<?php
class Dashboard extends CI_Controller
{
function __construct()
{
parent::__construct();
if(!$this->session->userdata('admin'))
redirect('Login');
}
function index()
{
$this->load->view('admin/dashboard');
}
}
?>
和管理模型如下:
<?php
class Admin extends CI_Model
{
function validate($username, $password)
{
$this->db->select('*')
->from('admins')
->where('username', $username)
->where('password', md5($password));
$query = $this->db->get();
return $query->result();
}
}
?>
- 3 回答
- 0 关注
- 160 浏览
添加回答
举报