user_model.php
function Login
($options = array()) {
// required values
if(!$this->_required(
array('user_login', 'user_password'), $options)
) return false;
$user = $this->GetUsers(array('user_login' => $options['user_login'], 'user_password' => md5($options['user_password']))); if(!$user) return false;
$this->session->set_userdata('user_login', $user->user_login);
$this->session->set_userdata('user_id', $user->user_id);
$this->session->set_userdata('user_type', $user->user_type);
return true;
}
function GetUsers
($options = array()) {
// Qualification
if(isset($options['user_id'])) $this->db->where('user_id', $options['user_id']);
if(isset($options['user_login'])) $this->db->where('user_login', $options['user_login']);
if(isset($options['user_email'])) $this->db->where('user_email', $options['user_email']);
if(isset($options['user_password'])) $this->db->where('user_password', $options['user_password']);
if(isset($options['user_status'])) $this->db->where('user_status', $options['user_status']);
if(isset($options['user_type'])) $this->db->where('user_type', $options['user_type']);
// Limits / offset
if(isset($options['limit']) && isset($options['offset'])) $this->db->limit($options['limit'], $options['offset']);
else if(isset($options['limit'])) $this->db->limit($options['limit']);
// Sort
if(isset($options['sortBy']) && isset($options['sortDirection'])) $this->db->order_by($options['sortBy'], $options['sortDirection']);
if(!isset($options['user_status'])) $this->db->where('user_status !=', 'deleted'); $query = $this->db->get("users");
if(isset($options['count'])) return $query->num_rows();
if(isset($options['user_id']) || isset($options['user_login'])) return $query->row(0);
return $query->result();
}
Controller main.php
function login()
{
$user_login = $this->session->userdata('user_login');
$this->form_validation->set_rules('user_login', 'login', 'trim|required|callback_check_login');
$this->form_validation->set_rules('user_password', 'password', 'trim|required');
$wysw = true;
if($this->form_validation->run())
{
$_POST['user_login'] = $this->db->escape_str($_POST['user_login']);
// je�eli formularz jes poprawny to:
if($this->user_model->Login(array('user_login' => $this->input->post('user_login'), 'user_password' => $this->input->post('user_password')))) {
$query = $this->db->query("SELECT * FROM ci_users WHERE user_login = '".$_POST['user_login']."'");
foreach($query->result() as $row)
if($row->user_status == 'inactive'){
$this->session->sess_destroy();
$info = array('info' => 'Twoje konto jest nie aktywne !', 'error' => true, 'red' => ''); $this->load->view('users/user_info', $info);
$wysw = false;
}else{
$info = array('info' => 'Zostałeś poprawnie zalogowany.', 'error' => false, 'red' => 'news'); $this->load->view('users/user_info', $info);
}
}else {
redirect('login');
}
}
if($wysw){
$this->load->view('main/login_form');
}
}else{
$info = array('info' => 'Jesteś już zalogowany !', 'error' => false, 'red' => 'news'); $this->load->view('users/user_info', $info);
}
}
function logout()
{
$this->session->sess_destroy();
$info = array('info' => 'Zostałeś poprawnie wylogowany.', 'error' => false, 'red' => 'news'); $this->load->view('users/user_info', $info);
}