- Impact
- 324
Got a tad bored today, so wrote a function for random passwords. Maybe it will help someone. :tu:
PHP:
<?php
/**
* Generate a random password.
*
* @param integer $numchars How long do we need the password to be?
* @param boolean $specialchars Include the special characters?
* @param boolean $extrashuffle Include an extra randomization on the password string?
* @return string
*/
function random_pass($numchars = 8, $specialchars = true, $extrashuffle = false)
{
$numchars = intval($numchars);
$numchars = ($numchars > 16 OR $numchars < 8) ? 8 : $numchars;
$chars = array_merge(range('a', 'z'), range(0, 9));
if ($specialchars)
{
$chars = array_merge($chars, array('!', '$', '_', '-', '#', '@'));
}
shuffle($chars);
$pass = '';
for ($i = 0; $i <= $numchars; $i++)
{
$pass .= $chars[$i];
}
if ($extrashuffle)
{
return str_shuffle($pass);
}
return $pass;
}
// Example, returns: 3ck#4sib2
echo random_pass(8, true, true);
?>