| Danltn.com Name: Daniel Neville Location: Danltn.com / Nottingham, UK Join Date: May 2007
Posts: 1,185
NP$: 0.56 ( Donate)
| PHP Code: <?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?
* @param boolean $mixedcase Mixed case or solely lowercase?
* @return string
*/
function random_pass($numchars = 8, $specialchars = true, $extrashuffle = false, $mixedcase = true)
{
$numchars = intval($numchars);
$numchars = ($numchars > 16 OR $numchars < 8) ? 8 : $numchars;
$chars = array_merge(range('a', 'z'), range(0, 9));
if ($mixedcase) /* Extra case */
{
$chars = array_merge($chars, range('A','Z'));
}
if ($specialchars)
{
$chars = array_merge($chars, array('!', '$', '_', '-', '#', '@'));
}
shuffle($chars);
$pass = '';
for ($i = 0; $i < $numchars; $i++) /* Changed to < only sign, otherwise it would be 1 character longer than needed */
{
$pass .= $chars[$i];
shuffle($chars); /* Get repeated characters, would add as an option, but I'm lazy */
}
if ($extrashuffle)
{
return str_shuffle($pass);
}
return $pass;
}
// Example, returns: 3ck#4si2
echo random_pass(8, true, true, true);
?>
Added an extra shuffle after each character, so you can get repeated characters too. (I prefer it that way).
Added MiXeD case option
Fixed the length ($i = 0; $i < =$numchars) part. |