This function generated a random password of a desired length. The default length is 10 but you can easily change the length by passing the desired length to the function.
function randomPassword ($passwordLength = 10) {
// First we start with an empth password
$randPassword = '';
// Then we say which characters we want to have in the random password
$allowedCharacters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// We add the desired number of characters to the password
for ($i=0; $i < $passwordLength; $i++) {
// We randomly choose a new character
$character = substr($allowedCharacters, mt_rand(0, strlen($allowedCharacters)-1), 1);
// And add it to the randmom password
$randPassword .= $character;
}
// Simply return the generated password and DONE
return $randPassword;
}
?>
And this is how it is being used.
Save the function in a file and call it password.php
Then in the same folder create a file and call it passwordtest.php
In the file password test write the following code
include('password.php');
echo "A random password with default length: ";
echo randomPassword();
echo "
A random password with a length of 6 characters: ";
echo randomPassword(6);
?>
Hope you can use this function.