IT & Programming

Generate Random Password

  • Generate random password.
  • Generate random string.
  • Generate verification code.

Function accepts a length parameter and returns randomly generated password. An optional length parameter can be passed to generate password with a specific length.

Function

function generatePassword($length = 5){

    $password = "";
    $possible = "0123456789bcdfghjkmnpqrstvwxyz";
    $i = 0;
    while ($i < $length) {
        $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
        if (!strstr($password, $char)) {
            $password .= $char;
            $i++;
        }
    }
    return $password;
}

Calling The Function

echo generatePassword($length = 10);

Output

df1h1rtere

Leave A comment

Email address is optional and will not be published. Only add email address if you want a reply from blog author.
Please fill required fields marked with *