Reputation: 16793
I have put together this little script which uses a list of words to generate a readable password.
The first part of the code works fine by generating the word and number. The problem is with the symbols at the end only seems to work every so often.
<?php
function random_readable_pwd($length=10){
// the wordlist from which the password gets generated
// (change them as you like)
$words = 'AbbyMallard,AbigailGabble,AbisMal,Abu,Adella,TheAgent,AgentWendyPleakley,Akela,AltheAlligator,Aladar,Aladdin,AlamedaSlim,AlanaDale,Alana,Alcmene,Alice,AmeliaGabble,AmosSlade,Amphitryon,AnastasiaTremaine,Anda,Andrina,Angelique,AngusMacBadger';
// Split by ",":
$words = explode(',', $words);
if (count($words) == 0){ die('Wordlist is empty!'); }
// Add words while password is smaller than the given length
$pwd = '';
while (strlen($pwd) < $length){
$r = mt_rand(0, count($words)-1);
$pwd .= $words[$r];
}
$num = mt_rand(1, 99);
if ($length > 2){
$pwd = substr($pwd,0,$length-strlen($num)).$num;
} else {
$pwd = substr($pwd, 0, $length);
}
$pass_length = strlen($pwd);
$random_position = rand(0,$pass_length);
$syms = "!@#$%^&*()-+?";
$int = rand(0,51);
$rand_char = $syms[$int];
$pwd = substr_replace($pwd, $rand_char, $random_position, 0);
return $pwd;
}
?>
<html><head><title>Password generator</title></head>
<body>
<h1>Password generator</h2>
<p>
<?php
echo random_readable_pwd(10);
?>
</p>
</body>
</html>
Upvotes: 2
Views: 1829
Reputation: 6346
you seem to be getting a random number between 0 and 51, but there are only 13 characters in the $syms string. It should be:
$syms = "!@#$%^&*()-+?";
$int = rand(0,12);
$rand_char = $syms[$int];
Haven't tested this, but I think this is the issue.
Or even better, get the string's length:
$syms = "!@#$%^&*()-+?";
$int = rand(0,strlen($syms)-1);
$rand_char = $syms[$int];
Upvotes: 6