stutstut.dev

Snippet: Password generator for PHP

Really short snippet today – a password generator. Not much to say about this one since it’s pretty simple. Pass it the length of password you want and optionally a string of valid characters it can use and it’ll give you a random string back.

function GeneratePassword($len, $allowedchars = false)
{
  if ($allowedchars === false) {
    $allowedchars = 'abcdefghijklmnopqrstuvwxyz01234567890';
  }
  $retval = '';
  $maxidx = strlen($allowedchars) - 1;
  for ($i = 0; $i < $len; $i++) {
    $retval .= $allowedchars[rand(0, $maxidx)];
  }
  return $retval;
}

I’ve got something a bit meatier lined up for the next snippet so I recommend subscribing to the RSS feed to make sure you don’t miss it. As always comments, questions, suggestions and requests are welcomed.