在我的电视系统上,我尝试制作一个登录表单。这个表单应该导致重定向到管理面板,但缺少一些东西,我无法弄明白。我已经在数据库中添加了一些信息"用户"表。但是,即使我使用var_dump();
,来自MySQL的任何信息都不会显示我在控制器文件夹中的代码
class User extends Backend_Controller {
public function __construct() {
parent::__construct();
}
public function login() {
$this->load->helper('language');
$this->lang->load('form_validation', 'lithuanian');
$dashboard = 'admin/dashboard';
$this->user_m->loggedin() == FALSE || redirect($dashboard);
$rules = $this->user_m->rules;
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == TRUE) {
if ($this->user_m->login() == TRUE) {
redirect($dashboard);
} else {
$this->session->set_flashdata('error', 'Email/password are wrong');
redirect('admin/user/login', 'refresh');
}
}
$this->data['subview'] = 'admin/user/login';
$this->load->view('admin/_layout_modal', $this->data);
}
public function logout() {
$this->user_m->logout();
redirect('admin/user/login');
}
}
在核心文件夹MY_Model.php
中class MY_Model extends CI_Model {
protected $_table_name = '';
protected $_primary_key = 'id';
protected $_primary_filter = 'intval';
protected $_order_by = '';
public $rules = array();
protected $_timestamps = FALSE;
function __construct() {
parent::__construct();
}
public function get($id = NULL, $single = FALSE) {
if ($id != NULL) {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->where($this->_primary_key, $id);
$method = 'row';
} elseif ($single == TRUE) {
$method = 'row';
} else {
$method = 'result';
}
if (!count($this->db->ar_orderby)) {
$this->db->order_by($this->_order_by);
}
return $this->db->get($this->_table_name)->$method();
}
public function get_by($where, $single = FALSE) {
$this->db->where($where);
return $this->get(NULL, $single);
}
public function save($data, $id = NULL) {
//timestamps
if ($this->_timestamps == TRUE) {
$now = date('Y-m-d H:i:s');
$id || $data['created'] = $now;
$data['modified'] = $now;
}
//insert
if ($id === NULL) {
!isset($data[$this->_primary_key]) || $data[$this->_primary_key] == NULL;
$this->db->set($data);
$this->db->insert($this->_table_name);
$id = $this->db->insert_id();
}
//update
else {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->set($data);
$this->db->where($this->_primary_key, $id);
$this->db->update($this->_table_name);
}
return $id;
}
public function delete($id) {
$filter = $this->_primary_filter;
$id = $filter($id);
if (!$id) {
return FALSE;
}
$this->db->where($this->_primary_key, $id);
$this->db->limit(1);
$this->db->delete($this->_table_name);
}
}
在模型文件夹user_m.php
中<?php
class User_M extends MY_Model {
protected $_table_name = 'users';
protected $_order_by = 'name';
public $rules = array(
'email' => array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email|xss_clean'
),
'password' => array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required'
)
);
function __construct() {
parent::__construct();
}
public function login() {
$user = $this->get_by(array(
'email' => $this->input->post('email'),
'password' => $this->hash($this->input->post('password')),
), TRUE);
if (count($user)) {
//log in user
$data = array(
'íd' => $user->id,
'email' => $user->email,
'name' => $user->name,
'loggedin' => TRUE,
);
$this->session->set_userdata($data);
}
}
public function register() {
}
public function logout() {
$this->session->sess_destroy();
}
public function loggedin() {
return (bool) $this->session->userdata('loggedin');
}
public function hash($string) {
return hash('sha512', $string . config_item('encryption_key'));
}
}
在视图文件夹login.php中
<?php
if (validation_errors() !== '') {
echo "<div class='alert alert-warning alert-dismissible' role='alert'> <button type='button' class='close' data-dismiss='alert'><span aria-hidden='true'>×</span><span class='sr-only'>Close</span></button> <strong>Warning!</strong>";
echo validation_errors();
echo "</div>";
}
?>
<?php echo form_open(); ?>
<div class="form-signin">
<?php
$data = array(
'name' => 'email',
'class' => 'form-control',
'placeholder' => 'Email'
);
echo form_input($data);
$data = array(
'name' => 'password',
'class' => 'form-control',
'placeholder' => 'Password'
);
echo form_password($data);
echo form_submit('submit', $login, 'class="btn btn-lg btn-primary"');
echo anchor('admin/user/auth/forgot_password', $forget_password, 'class="pull-right"');
?>
</div>
<?php echo form_close(); ?>