CodeIgniter Forums
Passing two values to custom validation - 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: Passing two values to custom validation (/showthread.php?tid=5323)



Passing two values to custom validation - El Forum - 01-17-2008

[eluser]Unknown[/eluser]
Hi,

was just looking at some validation code I have on a login form, and it seemed like it could be cool to test whether the login/password combination provided was valid, and I wanted to do this inside a custom validation callback.

Any clever ways of doing this that spring to mind? It's a security-light internal thing so I don't want to install and configure auth packages just for this.

thanks
a


Passing two values to custom validation - El Forum - 01-17-2008

[eluser]Pascal Kriete[/eluser]
It's pretty simple - if your validation looks like this:
Code:
$rules['username'] = "trim|required|max_length[20]|xss_clean";
$rules['password'] = "required|callback_checkLogin";

You would make a checkLogin function that looks like this:
Code:
function checkLogin($pw)
    {
        $this->load->model('User_model');
        
        $un = $this->input->post('username');
        
        if($this->User_model->checkCredentials($un, $pw))
        {
            $this->session->set_userdata('logged_in', TRUE);
            return true;
        }
        else
        {
            $this->validation->set_message('checkLogin', 'Credentials do not match.');
            return false;
        }
    }
Then in your internal pages you would check if the session data is set to true - and if it isn't you would redirect.