You could help by saying what you mean by "the result is strange"
There is a increment of $tot at the end so it should be incrementing,
but is it? What does your echo statement say?
You could move most of the file checking out of the loop, test and open
before the loop, if the file is okay then enter the while loop, close
the file after the loop. That should male you code a bit clearer (and
avoid 50 million file writes!!!). Next indent appropriately that will
make it is easy to see what is going on.
graeme.
bo wrote:
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++;
}
?>
--
Experience is a good teacher, but she sends in terrific bills.
Minna Antrim
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php