Skip to main content
corrected code
Source Link

I have an answer which also changes the logic of your code but I think there is a good reason why you should consider it:

function passtest($pass) {
    $errors = array();
    if (empty($pass)) $errors.push('Password field is empty');
    if$errors[] (empty($pass))= $errors.push('Password field is empty');empty';
    if (ctype_alnum($pass)) $errors.push($errors[] = 'Password has special character');character';
    [...]

    return '<br />' . $errors.joinimplode('<br'<br>', />'$errors);
}

Since it appears like this is somehow shown to the user I would notifyabout all the errors that happened in choosing the password, so that they can all be corrected in a single try.

Another sidenote: Since your code reminds me of the time where I was just getting started and this seems to be at least somewhat related: Don't save passwords, save their hashes. And use a good hashing implementation. If you're using PHP >= 5.5, there is a really easy way.

And thanks to "Fge", I can also give you a slightly less easy way for earlier PHP versions (>= 5.3.7).

I have an answer which also changes the logic of your code but I think there is a good reason why you should consider it:

function passtest($pass) {
    $errors = array();
    if (empty($pass)) $errors.push('Password field is empty');
    if (empty($pass)) $errors.push('Password field is empty');
    if (ctype_alnum($pass)) $errors.push('Password has special character');
    [...]

    return '<br />' . $errors.join('<br />');
}

Since it appears like this is somehow shown to the user I would notifyabout all the errors that happened in choosing the password, so that they can all be corrected in a single try.

Another sidenote: Since your code reminds me of the time where I was just getting started and this seems to be at least somewhat related: Don't save passwords, save their hashes. And use a good hashing implementation. If you're using PHP >= 5.5, there is a really easy way.

I have an answer which also changes the logic of your code but I think there is a good reason why you should consider it:

function passtest($pass) {
    $errors = array();
    if (empty($pass)) $errors[] = 'Password field is empty';
    if (ctype_alnum($pass)) $errors[] = 'Password has special character';
    [...]

    return '<br />' . implode('<br>', $errors);
}

Since it appears like this is somehow shown to the user I would notifyabout all the errors that happened in choosing the password, so that they can all be corrected in a single try.

Another sidenote: Since your code reminds me of the time where I was just getting started and this seems to be at least somewhat related: Don't save passwords, save their hashes. And use a good hashing implementation. If you're using PHP >= 5.5, there is a really easy way.

And thanks to "Fge", I can also give you a slightly less easy way for earlier PHP versions (>= 5.3.7).

Source Link

I have an answer which also changes the logic of your code but I think there is a good reason why you should consider it:

function passtest($pass) {
    $errors = array();
    if (empty($pass)) $errors.push('Password field is empty');
    if (empty($pass)) $errors.push('Password field is empty');
    if (ctype_alnum($pass)) $errors.push('Password has special character');
    [...]

    return '<br />' . $errors.join('<br />');
}

Since it appears like this is somehow shown to the user I would notifyabout all the errors that happened in choosing the password, so that they can all be corrected in a single try.

Another sidenote: Since your code reminds me of the time where I was just getting started and this seems to be at least somewhat related: Don't save passwords, save their hashes. And use a good hashing implementation. If you're using PHP >= 5.5, there is a really easy way.