Jc John Jc John - 1 month ago 6
PHP Question

Callback function returning false CodeIgniter

Why is this my code returning false in my if condition at my callback code. I tried to var_dump every each of their values and

Here is the output of the var_dump

var_dump($old_password_hash); = string(32) "25d55ad283aa400af464c76d713c07ad"
var_dump($old_password_db_hash); = object(stdClass)#24 (1) { ["user_password"]=> string(32) "25d55ad283aa400af464c76d713c07ad" }


The two values doesnt satisfies in
if($old_password_hash != $old_password_db_hash) {


Here is my full code

public function oldpassword_check($old_password){
$id = $this->input->post('userid');

$old_password_hash = md5($old_password);
$old_password_db_hash = $this->prof_model->fetch_pwrod($id);

//var_dump($old_password_hash);

var_dump($old_password_db_hash);

if($old_password_hash != $old_password_db_hash) {

$this->form_validation->set_message('oldpassword_check', 'Old password not match');
return FALSE;

} else {

return TRUE;

}

}

Answer

$old_password_hash is a string

$old_password_db_hash is an object

They will never be equal. string never equals object.

That's why $old_password_hash != $old_password_db_hash is always true. And therefore FALSE is returned.

Proper check is:

// take `user_password` property of an object
if ($old_password_hash != $old_password_db_hash->user_password) {
  $this->form_validation->set_message('oldpassword_check', 'Old password not match');
  return FALSE;
}
else {
  return TRUE;
}
Comments