1

I'm trying to build a user login.

It's all working perfectly except the md5 password.

Basically I have a form which does validation (for valid email, and required fields) and then I have made a custom callback validation rule to compare the user details entered in the form to those in the database. If no details are found, the rule returns FALSE, if they are found, the user will be logged in.

Here's the function for the checking rule in my controller:

public function user_pass_check()
    {
        $db_users = $this->load->database('users', TRUE);
        $post_email = $this->input->post('email', TRUE);
        $post_password = $this->input->post('password', TRUE);

        $query = $db_users->query("SELECT id, email, password FROM useraccounts WHERE `email`='$post_email' AND `password`=md5('$post_password')");
        $result = $query->num_rows(); 

        if ($result == 0)
        {
            $this->form_validation->set_message('user_pass_check', 'Login failed! Please check your login details.');
            return FALSE;
        }
        else 
        {
            // Session cookie creation goes here //
            return TRUE;
        }
    }

I just get an error when I try and log in. It works perfectly if the password in the database is not an md5 and I remove the md5() around the $post_password in the query. Natrually I want passwords to be md5, I can't have exposed passwords in my database.

Also here's the rules for the form validation:

$this->form_validation->set_rules('email', 'email address', 'trim|required|valid_email|callback_user_pass_check');
$this->form_validation->set_rules('password', 'password', 'trim|required|md5|callback_user_pass_check');

Any ideas?

4
  • is your password saved as md5 in database..? Jan 24, 2012 at 12:19
  • 2
    $post_password = md5($this->input->post('password', TRUE)); should do it I think (and remove md5 from the query)
    – danneth
    Jan 24, 2012 at 12:52
  • I already sorted it. The password column in the database was incorrectly set to 12 characters. It works great now. Jan 24, 2012 at 13:24
  • Could you add that as the answer and accept it? That way, your question doesn't show up in the unanswered lists.
    – swatkins
    Jan 24, 2012 at 15:11

1 Answer 1

1
$post_password = md5($this->input->post('password', TRUE));

does the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.