1
如果我们连续运行下面的代码,有时我们发现它会在密码中产生一些不应该得到的空间。避免产生密码空间
谁能告诉如何避免从下面生成的密码代码空格字符:
下面是代码:
function generateRandomPassword($length) {
srand((double)microtime()*1000000);
$password = "";
$vowels = array("a", "e", "i", "o", "u");
$cons = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r",
"s", "t", "u", "v", "w", "tr", "cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "
st", "sp", "sw", "pr", "sl", "cl");
$num_vowels = count($vowels);
$num_cons = count($cons);
for($i = 0; $i < $length; $i++) {
$c = $cons[rand(0, $num_cons - 1)];
if (rand(0,1)) $c = strtoupper($c);
$v = $vowels[rand(0, $num_vowels - 1)];
if (rand(0,1)) $v = strtoupper($v);
$password .= $c . $v;
if (rand(0,4) == 0) $password .= rand(0,9);
}
return substr($password, 0, $length);
}
$pwd = generateRandomPassword(12);
echo $pwd;
基本调试:'如果(($ C == ' ')或($ V =='')){模具( “拿到索引$ I空间”);}'或任何。找出空间来自何处/何时,并检查当时系统的状态。 –
我看不出会发生什么。只是为了确保我不会错过某些显而易见的事情,我将它运行了100,000次迭代。没有空间。 – Steve
尽管如果上面的代码是一个精确的副本,在你的'$ cons'数组中,元素''st“'有一个换行符,这将是不可取的,并且可能被误认为是空格字符 – Steve