Welcome Guest, Not a member yet? Register   Sign In
Login Controller/Model
#1

[eluser]wiredesignz[/eluser]
Session library must be autoloaded.

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Login extends Controller {

    function Login()
    {
        parent::Controller();
        $this->load->model('security');
    }
    
    function index()
    {
        if ($_POST)
        {
            $attempt->username = $this->input->post('username', TRUE); //use XSS filter
            $attempt->password = md5($this->input->post('password', TRUE)); //hash the password
            
            if ($this->security->try_login($attempt))
            {        
                redirect('home');
            }
        }
        
        $data = array(
            'username' => '',
            'password' => '',
            'message' => 'Enter your Username & Password to continue'
        );
        
        $this->load->view('login', $data);
    }
}
?>

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Security extends Model {
    
    function Security()
    {
        parent::Model();
    }
    
    function current_user()
    {
        $user = $this->session->userdata('user');
        
        //check if current user detail is not changed/deleted
        if ($this->try_login($user))
        {
            return $this->session->userdata('user');
        }
    }
    
    function try_login($attempt)
    {
        if ($attempt->password)    
        {
            //prevent SQL injection in username
            $attempt->username = $this->db->escape($attempt->username);

            //find username
            $query = $this->db->query("SELECT * FROM `users` WHERE `username` = {$attempt->username}");
            $user = $query->row();
            
            //check password & create user object in session if ok
            if ($user->password == $attempt->password)    
            {
                $user->category = strToLower($user->category); //user role
                $this->session->set_userdata('user', $user);
                return TRUE;
            }
        }
        
        //otherwise bail
        $this->session->sess_destroy();
        redirect('login');
    }
}
?>

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Home extends Controller {

    function Home()
    {
        parent::Controller();
        $this->load->model('security');
     }
    
    function index()
    {        
        $user = $this->security->current_user();

Any thoughts?


Messages In This Thread
Login Controller/Model - by El Forum - 11-27-2007, 08:14 PM
Login Controller/Model - by El Forum - 11-28-2007, 12:29 AM
Login Controller/Model - by El Forum - 11-28-2007, 01:43 AM
Login Controller/Model - by El Forum - 11-28-2007, 02:53 AM
Login Controller/Model - by El Forum - 11-28-2007, 03:21 AM
Login Controller/Model - by El Forum - 11-28-2007, 06:14 PM
Login Controller/Model - by El Forum - 11-28-2007, 06:23 PM
Login Controller/Model - by El Forum - 11-28-2007, 09:21 PM
Login Controller/Model - by El Forum - 11-28-2007, 10:53 PM
Login Controller/Model - by El Forum - 11-30-2007, 08:05 AM



Theme © iAndrew 2016 - Forum software by © MyBB