php-windows Digest 7 Jun 2005 14:46:09 -0000 Issue 2687

Topics (messages 26072 through 26078):

what's wrong with this while loop?
        26072 by: bo
        26073 by: M. Sokolewicz
        26074 by: Murray . PlanetThoughtful
        26075 by: Luis Moreira
        26077 by: graeme

cannot load dynamic module ..
        26076 by: martin hochreiter

Re: How to setup the mail server so that I can use mail() function
        26078 by: ST Ooi

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [email protected]


----------------------------------------------------------------------
--- Begin 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++;
}

?> 


--- End Message ---
--- Begin Message --- the problem with your loop is that after php enters it, it can't get out because there is no break, nor a way to turn the expression to false.

Normally, you have a loop which always evaluates to TRUE, and when it doesn't, you want to quit. And even when this is not how you did it, eg with while(true){ ... }, then you can bail out using break.

However, you did none of those here.
Your loop starts with
while($tot < $sum) {
        [...]
}
and doesn't in any way modify the value of any of both inside the (double!) loop, nor does it ever break. So, you're stuck with an eternal loop as a result.

Besdies that, I'm wondering what you need the while()-loop for anyway, since it's utterly useless in your code. An easier way would be to code it like this:

$rand_pass = '';
for ($i = 0; $i < $length; $i++){
   $rand_pos  = rand(0, $rand_max);
   $rand_key .= $key_chars{$rand_pos};
}

$somecontent = "new key: $rand_pass\n";
echo $somecontent;

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++;
}

?>

--- End Message ---
--- Begin 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!

Hi,

Don't have time at the moment to test out your code (hopefully someone else
will), but you may want to have a look at the str_shuffle() function, since
all you're doing is randomizing a known string.

Something like:

$key_chars =
str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
);

$random_password = substr($key_chars, 0, 15);

...should do the trick.

Can't comment on the randomization performed by str_shuffle, though, and do
you honestly need 50 million predetermined passwords?

Regards,

Murray

--- End Message ---
--- Begin Message ---
What's wrong with the while loop ?
You mean besides the fact that you are accessing an array and not specifying the index ?

$rand_key[] = $key_chars{$rand_pos};

Other than that, saying te result is strange is not good enough.
If you want help, you have to be more specific.
As to the "50 million question", it depends on the quality of the random number 
generator, but a fair guess is yes.



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++;
}

?>


--- End Message ---
--- Begin Message ---
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

--- End Message ---
--- Begin Message ---
Hi!

I installed Apache 2.054 and the latest version of php 4.
Unfortunately I php can't load the extensions while startup of Apache.
Although the extension path is correct and the files exists.
I tried it with
H:\programme\php\extensions
H:\programme\php\extensions\
H:/programme/php/extensions
H:/programme/php/extensions/

without success.

Any hints?

lg

--- End Message ---
--- Begin Message --- As a matter of fact, I have install XMail and try to use PHPMailer to send the email but it's always rejected by the mail server. The reason is relay failed. I have a webmail software that running on the same server call UebiMiau which uses PHPMailer to send email, and it works. The code that I use are as follows.

<?php
require("class.phpmailer.php");

   $mail = new PHPMailer();
   $mail->IsSMTP();
   $mail->IsHTML(false);
   $mail->Charset = "iso-8859-1";
   $mail->timezone = date("O");
   $mail->From = '[EMAIL PROTECTED]';
   $mail->FromName = "ST Ooi";
   $mail->Host = "localhost";
   $mail->Port = 25;
   $mail->Hostname = "localhost";
   $mail->Subject = "Send mail from laser-compo.com";
   $mail->Body = stripslashes("Send mail trial");
   $mail->AddAddress('[EMAIL PROTECTED]', 'Patrick Ooi');
   $mail->AddReplyTo('[EMAIL PROTECTED]', 'ST Ooi');
   $mail->Send()
?>


Any help would be appreciated.

Thanks

ST Ooi

ST Ooi wrote:
Hi,

How can I setup my mail server so that I can use mail() function in PHP? I'm running WindowsXP Pro.

Thanks

ST Ooi

--- End Message ---

Reply via email to