note 74498 deleted from function.rand by bjori
| From: | bjori@php.net | Date: | Sat, 14 Apr 2007 09:52:42 +0000 |
| Subject: | note 74498 deleted from function.rand by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125390@lists.php.net to get a copy of this message | ||
Note Submitter: marcus_marzullus at email dot it
Reason: integrated
----
dxillusion Key generator function has a nice bug ;-)
$r = rand(0,count($keyVals)); //WRONG
$r = rand(0,count($keyVals)-1); // CORRECT
Here the function with bug fixed.
function mKey($len = 12, $type = 'ALNUM')
{
// Register the lower case alphabet array
$alpha = array('a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm',
'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x',
'y', 'z');
// Register the upper case alphabet array
$ALPHA = array('A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M',
'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z');
// Register the numeric array
$num = array('1', '2', '3', '4', '5',
'6', '7', '8', '9', '0');
// Initialize the keyVals array for use in the for loop
$keyVals = array();
// Initialize the key array to register each char
$key = array();
// Loop through the choices and register
// The choice to keyVals array
switch ($type)
{
case 'lower' :
$keyVals = $alpha;
break;
case 'upper' :
$keyVals = $ALPHA;
break;
case 'numeric' :
$keyVals = $num;
break;
case 'ALPHA' :
$keyVals = array_merge($alpha, $ALPHA);
break;
case 'ALNUM' :
$keyVals = array_merge($alpha, $ALPHA, $num);
break;
}
// Loop as many times as specified
// Register each value to the key array
for($i = 0; $i <= $len-1; $i++)
{
$r = rand(0,count($keyVals)-1);
$key[$i] = $keyVals[$r];
}
// Glue the key array into a string and return it
return join("", $key);
}
?>