what's wrong with this while loop?
| From: | bo | Date: | Tue, 07 Jun 2005 04:58:15 +0000 |
| Subject: | what's wrong with this while loop? | ||
| Groups: | php.windows | ||
| Request: | Send a blank email to php-windows+get-26072@lists.php.net to get a copy of this message | ||
Following my code is to write a 15digit random generated password into a
file named test.txt. I was trying to keep it random generating 100 passwords
and be written into test.txt by using a while loop, however, the result is
strange.
Take a look at the code (most of it is from php.net) and what's wrong with
this while loop?
besides, if I generate a large sum of passwords,say 50 million, will it be
repetitive?
Thank you!
<?php
/*
<meta HTTP-EQUIV="REFRESH"
content="1; url=http://svr/rand.php">
*/
$length = 15;
$sum = 100;
$key_chars =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$rand_max = strlen($key_chars) - 1;
$filename = 'test.txt';
$tot=0;
while($tot < $sum){
for ($i = 0; $i < $length; $i++){
$rand_pos = rand(0, $rand_max);
$rand_key[] = $key_chars{$rand_pos};
}
$rand_pass = implode('', $rand_key);
$somecontent = "$tot $rand_pass\n";
echo $somecontent;
$filename = 'test.txt';
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
$tot++;
}
?>