PHP: verificare la robustezza di una password

PHP: verificare la robustezza di una password

Con PHP possiamo verificare la robustezza di una password.

La soluzione è la seguente:


function check_pwd($password) {
    $errors = [];
    if (preg_match('/\s/', $password)) {
        $errors[] = 'Spaces are not allowed.';
    }
    if(strlen($password) < 12) {
       $errors[] = 'At least 12 characters.'; 
    }

    if (!preg_match('/(?=.*[a-z])(?=.*[A-Z])/', $password)) {
        $errors[] = 'Password should include uppercase and lowercase characters.';
    }

    $found_digits = preg_match_all('/\d/', $password, $matches);

    if ($found_digits < 2) {
       $errors[] = 'Password should include at least 2 digits.';         
    }

    $found_symbols = preg_match_all('/[@%&!*(){}<>\[\]^\?\/|#]/', $password, $matches);

    if ($found_symbols < 1) {
       $errors[] = 'Password should include at least 1 special character.';         
    }

    return $errors;    
}

Torna su