Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the following php function that takes one parameter, a password: functi

ID: 3810154 • Letter: C

Question

Complete the following php function that takes one parameter, a password:

function checkPassword($password) {

//function checks to see if the password is a word in the following list of common passwords. It also checks the length of the password. The password is illegal if it is the list of common passwords, OR is shorter than 8 characters. The function returns false if the password is illegal, and returns true otherwise.  

$commonPasswords = array('password', '123456', 'abc123', 'qwerty', '1234', 'baseball', 'football', 'dragon', 'monkey');

Explanation / Answer

The desired function is given below. Here we have initially checked the length of password, if its less then 8 then return false. After that we will check for the common password by matching the array string with password.

function checkPassword($password) {

$commonPasswords = array('password', '123456', 'abc123', 'qwerty', '1234', 'baseball', 'football', 'dragon', 'monkey');
if (strlen($password) < 8) {
        echo 'Password is short!!';
       return false;
    }
foreach($commonPasswords as $string)
{
if(strpos($password, $string) !== false)
{
    echo 'Its very common password';
    return false;
}
}
   return true;
}