2

So I'm busy on this system to change your password, when the password is changed, a green box appears with the message 'Your password has been changed.'

When there's an error, a red box appears with the error, for example: "Your current password is incorrect", or "Your new password and the verification password aren't the same.".

So when there's a post request, I count the errors, if there's more then 0, return the errors like this:

public function nieuwpw($username, $currentpass, $newpass, $newpassconf)
{
    $this->errors[] = array();
    $this->succes[] = array();
    $query = $this->db->conn->prepare('SELECT pass FROM ht_users WHERE naam = ?');
    $query->bind_param('s', $username);
    $query->execute();
    $query->bind_result($dbpass);
    $query->store_result();
    while ($query->fetch()) {
        if (password_verify($currentpass, $dbpass))
        {
            if ($newpass === $newpassconf)
            {
                $newpasshash = password_hash($newpass, PASSWORD_DEFAULT);
                $stmt = $this->db->conn->prepare('UPDATE ht_users SET pass = ? WHERE naam = ?');
                $stmt->bind_param('ss', $newpasshash, $username);
                $stmt->execute();
                $stmt->close();
            }
                else
            {
                $this->errors[] = 'Whoops, je 2 nieuwe wachtwoorden zijn niet gelijk.';
            }
        }
            else
        {
            $this->errors[] = 'Whoops, je huidige wachtwoord klopt niet!';
        }
    }
    if (count($this->errors) > 0)
    {
        return $this->errors;
    }
    $this->succes[] = 'Je wachtwoord is met success gewijzigd!';
    return $this->succes;
    $query->close();
}

}

Thats the function I use to change passwords. The following is how I 'echo' the errors:

        <?php //24
//25
if(isset($users->succes))//26
{//27
    if (count($users->succes) > 0) //28
    {//29
        foreach ($users->succes as $succes) {//30
                    $content = '<div id="message_succes" style="background-color:#00B200; width:100%; height:30px;"><p style="color:#fff; font-family:Ubuntu; padding:5px;">' . $succes. '</p></div>';//31
    echo $content;//32
//33
            }   //34
    }//35
}//36
if(isset($users->errors))//37
{//38
    if (count($users->errors) > 0) //39
    {//40
        foreach ($users->errors as $errors) {//41
                    $content = '<div id="message_succes" style="background-color:#ff0033; width:100%; height:30px;"><p style="color:#fff; font-family:Ubuntu; padding:5px;">' . $errors. '</p></div>';//42
    echo $content;//43
//44
            }   //45
    }//46
}//47
//48
?> //49

And still, even tho I only echo the errors, or the success message, if there's more then 1, I get these errors:

Notice: Array to string conversion in C:\xampp\htdocs\pages\page16.php on line 31


Notice: Array to string conversion in C:\xampp\htdocs\pages\page16.php on line 42

In the second code block, I numbered the lines so you can see which lines throw the errors.

When doing a var_dump, I get the following output:

array(2) { [0]=> array(0) { } [1]=> string(41) "Whoops, je huidige wachtwoord klopt niet!" }

Also, what I find really weird is that it shows the red error box twice, and even tho there isn't any success message, it also shows the green box...

I hope that this'll be considered a complete question and someone can help me solve this problem. Thanks.

2 Answers 2

1

I think the cause of the error is in line 3 and 4 of the first code block you posted:

$this->errors[] = array();
$this->succes[] = array();

Try changing those lines into:

$this->errors = array();
$this->succes = array();

In your code you create an array and immediately set the first element of the array to be an array. You can see that in the var_dump you posted:

array(2) { [0]=> array(0) { } [1]=> string(41) "Whoops, je huidige wachtwoord klopt niet!" }

Later on, when you loop over either the $errors or $succes array, the first iteration of the loop gives you back the array you set as the first element of the $errors or $succes array. Then you try to echo that array and php gives you a notice that you are echoing an array.

Does my suggested fix work and do you see why your code breaks?

PS:

  1. Try to keep all your code in English so it's easier for non-Dutch speaking persons to understand your code.
  2. The following string is written in Dutch: 'Je wachtwoord is met success gewijzigd!'. In Dutch, 'succes' is written with 1 's', so it should be ''Je wachtwoord is met succes gewijzigd!'.

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

1

have you tried using it like this ??

$this->errors = array();
$this->succes = array();

and

    foreach ($users as $user) {
        if(isset($user->succes))
        {
            $content = '<div id="message_succes" style="background-color:#00B200; width:100%; height:30px;"><p style="color:#fff; font-family:Ubuntu; padding:5px;">' .$user->succes. '</p></div>';
            echo $content;

        }  
        if(isset($user->errors))
        {
            $content = '<div id="message_succes" style="background-color:#ff0033; width:100%; height:30px;"><p style="color:#fff; font-family:Ubuntu; padding:5px;">' . $user->errors. '</p></div>';
            echo $content;

        }   
    }

comment if any errors...

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.