[eluser]benfike[/eluser]
Login things from ion_auth with modify:
Code:
class Auth
{
public function __construct()
{
$this->ci =& get_instance();
$this->ci->load->library('email');
$this->ci->load->library('session');
$this->ci->load->helper('cookie');
//autologin if remembered
if (!$this->logged_in() && get_cookie('username') && get_cookie('password'))
{
$this->ci->auth = $this;
$this->login_remembered_user();
}
}
public function login($username, $password, $remember=FALSE)
{
if (empty($username) || empty($password))
{
return FALSE;
}
$query = $this->db->select('id,username,password,admin')
->where('username', $username)
->where('password', md5($password))
->where('status', 'active')
->limit(1)
->get('users');
$result = $query->row();
$this->update_last_login($result->id);
$session_data = array(
'username' => $result->username,
'id' => $result->id,
'user_id' => $result->id
);
if ($result->admin == 'true') {
$session_data['admin'] = 'TRUE';
}
$this->session->set_userdata($session_data);
if ($remember)
{
$this->remember_user($result->id);
}
}
public function login_remembered_user()
{
//check for valid data
if (!get_cookie('username') || !get_cookie('password'))
{
return FALSE;
}
$query = $this->db->select('username,id,password,admin')
->where('username', get_cookie('username'))
->where('password', md5(get_cookie('password')))
->limit(1)
->get('users');
//if the user was found, sign them in
if ($query->num_rows() == 1)
{
$user = $query->row();
$this->update_last_login($user->id);
$session_data = array(
'username' => $user->usernam,
'id' => $user->id,
'user_id' => $user->id
);
if ($result->admin == 'true') {
$session_data['admin'] = 'TRUE';
}
$this->session->set_userdata($session_data);
//extend the users cookies
$this->remember_user($user->id);
}
}
/**
* remember_user
*
* @return bool
* @author Ben Edmunds
**/
private function remember_user($id)
{
if (!$id)
{
return FALSE;
}
$user = $this->get_user($id)->row();
$password = md5($user->password);
set_cookie(array(
'name' => 'username',
'value' => $user->username,
'expire' => 86500,
));
set_cookie(array(
'name' => 'password',
'value' => $password,
'expire' => 86500,
));
}
public function update_last_login($id)
{
$this->load->helper('date');
$this->db->update('users', array('last_login_at' => now()), array('id' => $id));
return $this->db->affected_rows() == 1;
}
public function logged_in()
{
return (bool) $this->ci->session->userdata('username');
}
public function get_user($id = false)
{
//if no id was passed use the current users id
if (empty($id))
{
$id = $this->session->userdata('user_id');
}
$this->db->where('users.id', $id);
$this->db->limit(1);
return $this->db->get('users');
}
}
controller
Code:
function bejelentkezes()
{
//validate form input
$this->validation->set_rules('username', 'Username', 'required');
$this->validation->set_rules('password', 'Password', 'required');
if ($this->validation->run() == true)
{ //check to see if the user is logging in
//check for "remember me"
$remember = (bool) $this->input->post('remember');
if ($this->auth->login($this->input->post('username'), $this->input->post('password'), $remember))
{ //if the login is successful
header("Location: ".$_POST['referer']);
}
else
{ //if the login was un-successful
redirect('csapat/keret', 'refresh'); //use redirects instead of loading views for compatibility with MY_Controller libraries
}
}
}