我有个问题,共同创始人3。 我在使用wampserver在lovalhst。 我的自动加载文件
$ autoload ['libraries'] = array('database','form_validation', 'session','MY_Router');
我的用户模型( Users_model.php ):
class Users_model extends CI_Model{
public function login_user($username, $password){
$this->db->where('username', $username);
$this->db->where('password',md5($this->input->post('password')));
$result = $this->db->get('user');
if ($result->num_rows() == 1) {
return $result->row();
$data = array(
'id' => $result->id,
'username' => $result->username,
'isadmin' => $result->is_admin,
'LastOnline'=>date('Y-m-d H:i:s')
);
$this->session->set_userdata($data);
$this->db->where('id',$result->id);
$this->db->update('user', array('LastOnline'=> date('Y-m-d H:i:s')));
}else {
return false;
}
}
}
我的用户控制器(users.php)
class Users extends CI_Controller{
//function constructe
public function __construct() {
parent::__construct();
$this->load->model('Users_model');
}
public function login(){
$this->load->view('users/login_view');
$this->form_validation->set_rules('username', 'Username', array('trim', 'min_length[5]', 'required'));
$this->form_validation->set_rules('password', 'Password', array('trim', 'min_length[5]', 'required'));
if ($this->form_validation->run() == FALSE) {
$this->form_validation->set_error_delimiters('<div class="alert alert-danger" style="width: 95%;margin: 0 auto;margin-bottom:5px;"><strong>Error:</strong>', '</div>');
$data = array(
'errors' => validation_errors()
);
$this->session->set_flashdata($data);
}else {
$username = $this->input->post('username');
$password = $this->input->post('password');
$validate_user = $this->Users_model->login_user($username,$password);
if ($validate_user->num_rows > 0) {
$users_data = $validate_user->row_array();
$username = $users_data['username'];
$email = $users_data['email'];
$level = $users_data['user_level'];
$SessionData = array(
'username' => $username,
'email' => $email,
'level' => $level,
'username' => $username,
'logged_in' => TRUE
);
$this->session->set_userdata($SessionData);
if($level === 1){
redirect('admin/dashboard_view');
} else{
redirect('users/profile');
}
}else {
$this->session->set_flashdata('login_fail', 'faleed');
}
}
}
}