Jump to content

[SOLVED] random letters?


ohdang888

Recommended Posts

i leave out ambiguous characters like 1, l (lower case L), 0, o, etc... i also don't allow duplicates, which you can easily change.

 

function generatePassword ($length =  {

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

  // define possible characters
  $possible = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"; 
    
  // 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;

}

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.