CodeIgniter Forums
Username and password authentication script - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Username and password authentication script (/showthread.php?tid=11931)



Username and password authentication script - El Forum - 09-29-2008

[eluser]bhakti.thakkar[/eluser]
Dear all,
I am a new bie to CI. can some one help me with complete code of validating a username script. if username present in the DB then make entry in the LoginHistory_T table and redirect him to the main.php and if not give him a error message.

My three files (MVC) are:
mlogin.php
login_view.php
login.php

mlogin.php code:
Code:
<?php

    class Mlogin extends Model
    {

        function __Construct()
        {
            parent::Model();
        }
        
        function get_users($username)
        {
            $this->load->database();
            $this->db->select('Username_VC');
            $this->db->from('Users_T');
            $query = $this->db->get();
            return $query->row();
        }
    
        function get_last_ten_entries()
        {
            $this->load->database();
            $query = $this->db->get('Users_T');
            return $query->result();
//            print $query->num_rows();
        }
        
        function check_login_data($username, $password)
        {    

            $query = $this->db->select('Username_VC, Password_VC');
            $query = $this->db->from('Users_T');
            $query = $this->db->where('Username_VC', $username);
//            $query = $this->db->where('Password_VC', $password);
            $query = $this->db->get();
            
            if($query->num_rows() > 0)
            { return $query->result(); }
            else
            { return false;}
        }
    }


    

    function addUsersHistory(){
      $now = date("Y-m-d H:i:s");
      $data = array(
        'Username' => $this->input->xss_clean($this->input->post('username')),
        'RemoteAddr_VC' => $this->input->ip_address(),
        'LoginDate_DT' => $now
      );

      $this->db->insert('LoginHistory_t', $data);
     }


?>

code in login.php

Code:
<?php

class Login extends Controller {

    function Login()
    {
        parent::Controller();    
        $this->load->helper('url');

    }
    
    function index()
    {
        $data['base']       = $this->config->item('base_url');
        $data['css']        = $this->config->item('css');        
        
        $this->load->helper(array('form', 'url'));        // load form
        $this->load->library('validation'); // load validations
              $rules['username'] = 'trim|required|max_length[12]|xss_clean|callback__check_logindata';
              $rules['password'] = 'trim|required|md5';

            // set values for repopulating fields

            $fields['username']='Username';
            $fields['password']= 'Password ';


            $this->validation->set_fields($fields);
              $this->validation->set_rules($rules);
            
    
        if ($this->validation->run() == TRUE)
        {            
            //$this->load->view('checkuser' , $data);
            $this->load->model('Mlogin','',TRUE);
            $password = $this->validation->password;
            $username = $this->validation->username;
            if ($this->Mlogin->check_login_data($username, $password) == FALSE)
            {
                $this->validation->set_message('_check_logindata', 'Username and Password might be incorrect!');
                return FALSE;
            }


//            $this->Mlogin->addHistory();
           // redirect('welcome/thankyou','refresh');



        }
        else
        {
            $this->load->view('login_view' , $data);
        }
    }


     function _check_logindata($username)
        {
          
        $username = $this->input->post('username');
        $password = $this->input->post('password');
            
            $this->load->model('Mlogin');
            if ($this->Mlogin->check_login_data($username, $password) == FALSE)
            {
                $this->validation->set_message('_check_logindata', 'Username and Password might be incorrect!');
                return FALSE;
            }
            else
            {
                return TRUE;
            }
        }

}
?>


there is some mistake somewhere. i get the error message:
Severity: Notice

Message: Undefined property: Mlogin::$db

Filename: models/mlogin.php

Line Number: 31


my DB connection is fine. i printed the number of records in a table and it did print

can some one help me to rectify my code or help me with some sample script link

Million Thanks in advance


Username and password authentication script - El Forum - 09-30-2008

[eluser]Phil Sturgeon[/eluser]
The main problem here is that you are not loading the database library in check_login_data(). You should load database in /config/autoload.php to save having to load it in each model method.

A few other small things...

get_users($username)
This name is confusing. Suggests it will return multiple but only returns 1.

get_last_ten_entries()
Function doesnt limit to 10 at all, will return all users.

check_login_data($username, $password)
Return the user id and check its over 0 in your controller or something similar, but there is no need to return the pass. Also, you are not actually checking the password, should probably uncomment that ^_^


Username and password authentication script - El Forum - 09-30-2008

[eluser]SitesByJoe[/eluser]
While we're picking apart the methods in the model...may I also suggest that you change

Code:
get_last_ten_entries()

To something more modular, like:

Code:
get_latest_entries($limit)

Then you could call the function like (from your controller):

Code:
$this->My_model->get_latest_entries(10);

You can use the same function, now with any amount.


Username and password authentication script - El Forum - 09-30-2008

[eluser]bhakti.thakkar[/eluser]
Thanks for your suggestions. i will surely take care of the things mentioned


Username and password authentication script - El Forum - 10-01-2008

[eluser]Vitek[/eluser]
I may be missing something, but is there is any reason for checking the user/pass combo twice, first via the validation callback then one more time after $this->validation->run() was a success?

EDIT: Forgot to mention, your _check_logindata() callback is passing the raw password to check_login_data(), as opposed to a hashed version.


Username and password authentication script - El Forum - 10-03-2008

[eluser]alaminx[/eluser]
the constructor : function Mlogin()


Username and password authentication script - El Forum - 10-11-2008

[eluser]Unknown[/eluser]
I need your login_view.php

Do you post it?!

Thank you!

I'm From Brazil
I don't speak english

=D


Username and password authentication script - El Forum - 01-18-2009

[eluser]d_anank[/eluser]
in the "function __Construct()"

probably you change this function to "Mlogin" or same as Class name