[eluser]riwakawd[/eluser]
[quote author="InsiteFX" date="1402147925"]
Code:
// $this->session->set_userdata($newdata); Now look at your code below!
$this->session->set_userdata('user_id') = $user_query->row('user_id');
[/quote]
I think I all most got it just trying now to get it redirect working.
Code:
public function index() {
$this->ControllerCommonLogin();
}
public function ControllerCommonLogin() {
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_checkinfo');
$data['title'] = $this->lang->line('heading_title');
$data['action'] = site_url('backend/login');
if($this->form_validation->run($this) == FALSE) {
$this->load->view('template/common/login', $data);
} else {
redirect('dashboard');
}
}
public function validate($password) {
$username = $this->input->post('username');
//query the database
$result = $this->user_model->login($username, $password);
if($result) {
$sess_array = array();
foreach($result as $row){
$sess_array = array(
'user_id' => $row->id,
'username' => $row->username,
'ip' => $row->ip
);
$this->session->set_userdata('isLogged', $sess_array);
}
return TRUE;
} else {
$this->form_validation->set_message('checkinfo', 'Invalid username or password');
return false;
}
}
}
Model
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model {
private $user_id;
private $username;
private $permission = array();
public function __construct() {
parent::__construct();
if(!empty($this->session->userdata('user_id'))) {
$user_query = $this->db->query("SELECT * FROM " . $this->input->post('db_prefix') . "user WHERE user_id = '" .$this->session->userdata('user_id') . "' AND status = '1'");
if($user_query->num_rows > 0) {
$this->user_id = $user_query->row('user_id');
$this->username = $user_query->row('username');
$this->db->query("UPDATE " . $this->input->post('db_prefix') . "user SET ip = '" . $this->input->ip_address()."' WHERE user_id = '" . (int)$this->session->userdata('user_id') . "'");
$user_group_query = $this->db->query("SELECT * FROM " . $this->input->post('db_prefix') . "user_group WHERE user_group_id = '" . (int)$query->row('user_group_id') . "'");
} else {
$this->logout();
}
}
}
function login($username, $password){
$this->db->select('user_id, username, password');
$this->db->from('user');
$this->db->where('username', $username);
$this->db->where('password', $this->bcrypt->hash_password($password));
$user_query = $this->db >get();
if($user_query->num_rows() == 0) {
return $user_query->result();
} else {
return false;
}
}
function logout() {
$this->session->unset_userdata('user_id');
$this->user_id = '';
$this->username = '';
}
function isLogged() {
return $this->user_id;
}
}