0
votes

I am making a little login system using php inside of Codeigniter framework. I using the encryption class to encode a password, store in database. Then on login, finds their encrypted password using their username, then matches it against the password they entered. The passwords ARE getting encoded during registration. And in the below code, I have been able to print out the encoded password from the database, but then it fails out whenever I run it through decode. But no error message, just nothing.

public function check_pw(){
    //post username and password to database
    $data['username'] = $this->input->post('username');
    $data['password'] = $this->input->post('password');
    $this->load->model('lindsdata');
    //encoded password returned from database - this is working
    $return_data = $this->lindsdata->password($data);
    //$return_data = $this->encrypt->decode($return_data);  
    //print_r shows $return_data does hold the encrypted password
    //but when I uncomment the line with decode in it, $return_data print_rs nothing. 
    print_r($return_data);
    if ($return_data == $data['password']) {
        redirect('/update/user', 'location');
    }
    if($return_data !== $data['password']){
        //at this point, $return_data of course still has nothing
        print_r($return_data);
        //print_r below is the entered password by user..that shows up.
        print_r($data['password']);
        $error['the_error'] = 'Your Credentials are wrong';
    }else{
        $error['the_error'] = '';
    }
}

I tried this below to make sure things were being encoded properly...this returned "test" as expected.

 $a = "test";
 $test = $this->encrypt->encode($a); 
 $test = $this->encrypt->decode($test);
 print_r($test);

To further confuse me...I originally manually encoded a password (like in the code above) and had put the result in a database with a user (I had not set up a registration form yet, just did that so I had a user to work with)...that one works fine for some reason. None of the other encoded user passwords are working. I don't get why they are failing out during the decode. The database is set to varchar(255). And I am DEFINITELY getting the encoded password when I run it through decode...but then nothing. No error message or anything and very stumped. Thanks it advance. ...and I did not post the model because I don't think it was relevant since that is indeed returning the encoded password for checking as it should.

EDIT - It doesn't make sense $return_data is printing the hash from the DB and $password is returning the proper password but $pw is returning nothing. ...could it be that my SQL database is somehow corrupting/editing my hash? This should work from what I can tell.

    $data['username'] = $this->input->post('username');
    $password = $this->input->post('password');
    $this->load->model('lindsdata');
    $return_data = $this->lindsdata->password($data);
    print_r($return_data);
    print_r($password);
    $pw = password_verify($password, $return_data);
    print_r($pw);

EDIT - My input to the database is quite simple... I see no reason for that having an issue. ...still failing on either decode or password_verify. Still no idea the problem. It is a simple Mysql database...

public function register_submit(){
    $data['user_name'] = $this->input->post('user_name');
    $pwd = $this->input->post('password');
    $data['password'] =  password_hash($pwd, PASSWORD_DEFAULT);
    $this->load->model('lindsdata');
    $this->lindsdata->register($data);      
}

function register($data){
    $this->db->insert('cred', $data);
}
1
Cannot produce the problem. Your test code is working fine - Shaiful Islam
What value is inserted at your database when password is 'test'; - Shaiful Islam
test I just did in browser, did not run it through the database. I should try that though. - Sean Anderson

1 Answers

0
votes

It is against security practices to directly store a password in the database, even if encrypted. If someone get's access to your server it can take both the code and a snapshot of the database and find out the passwords of all users registered on your site. The best practice regarding passwords is to save a hash of the password (preferably SHA256 or stronger), and compare the hash of the password the user enters in the login form with the one stored in the database. Or, even better, you can use the PHP built in password hashing support (http://php.net/manual/en/ref.password.php).

And indirectly, hashing the password will solve your problem :)