Next Live Event: NamePros Live Auction, May 23rd at 6PM EDT
Results from the May 8th live auction are here.
13 members in the live chat room. Join Chat!
Got a tad bored today, so wrote a function for random passwords. Maybe it will help someone.
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?
* @return string
*/
function random_pass($numchars = 8, $specialchars = true, $extrashuffle = false)
{
$numchars = intval($numchars);
$numchars = ($numchars > 16 OR $numchars < 8) ? 8 : $numchars;
/**
* 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;
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.