Home ›
Creating a random password... the Drupal wayCreating a random password... the Drupal way
Submitted by Benjamin Melançon on June 1, 2008 - 4:23am
Lesson of the day: don't reinvent Drupal functions.
<?php
/**
* Generate a really lame random password. This would be cooler with words.
*
* Still, gratitude to Jon Haworth, from whom this code was taken.
* <a href="http://www.laughing-buddha.net/jon/php/password/
" title="http://www.laughing-buddha.net/jon/php/password/
">http://www.laughing-buddha.net/jon/php/password/
</a> */
function changents_tl_au_rand_password($length = 5) {
// start with a blank password
$password = "";
// define possible characters
$possible = "23456789abcdefghijkmnpqrstuvwxyz";
// 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;
}
?>
Practically the same result - and almost the exact code - as Drupal's own user_password function. (Haworth's code does have a step to prevent a letter from being used twice, but I was considering taking that out anyway.)
http://api.drupal.org/api/function/user_password/5
So we'll just use that, and pass in the shorter length we want.
Resolution
Searched words:
make random password
generate pass
assorted string of letters
php random password generator
Comments
Post new comment