Jump to content

oh my god - this is crazy! lol


jwk811

Recommended Posts

im using this little random password generator this is its code -
[code] function makeRandomPassword() {
$salt = "abchefghjkmnpqrstuvwxyz0123456789";
  srand((double)microtime()*1000000); 
      $i = 0;
    while ($i <= 7) {
          $num = rand() % 33;
          $tmp = substr($salt, $num, 1);
            $pass = $pass . $tmp;
            $i++;
    }
    return $pass;
} [/code]
see where it says $pass = $pass . $tmp;? well where is it getting the $pass inside the variable? im getting an error saying undefined variable but the code is still working perfectly.. and when i get rid of the $pass it messes up and doesnt work... crazy huh? would you know whats wrong with this? this is actually something i found at this website and its working on some of my other script but not this one.. dont understand.. thanks for help!
Link to comment
https://forums.phpfreaks.com/topic/27285-oh-my-god-this-is-crazy-lol/
Share on other sites

The first time through, concatenate a null string and a random string.  Then the second time, it's got the value of the previous run.  It' works on SOME servers because the error reporting settings are lower on some servers.

it could be fixed (causing no errors) by adding $pass = ""; above the start of the while loop.

That way it wouldn't be a NULL string.
I've used this before, forget where I found it but it looks very similar to your code.  I did not get any errors when I used it.  It's also commented, which is nice.

[code]function generatePassword ($length = 8)
{

  // start with a blank password
  $password = "";

  // define possible characters
  $possible = "0123456789bcdfghjkmnpqrstvwxyz";
   
  // set up a counter
  $i = 0;
   
  // add random characters to $password until $length is reached
  while ($i < $length) {

    // pick a random character from the possible ones
    $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
       
    // we don't want this character if it's already in the password
    if (!strstr($password, $char)) {
      $password .= $char;
      $i++;
    }

  }

  // done!
  return $password;

}[/code]


Regards,
John Sladek

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.