[italy-tech] [oscailt] aggiungere un codice random agli articoli pubblicati
laxertu
laxertu at logorroici.org
Fri Sep 7 02:31:51 PDT 2007
io uso spesso questa funzione, da php.net:
non garantisce l'unicità, ma oltre ad essere estremamente improbabile
che accada, se gli utenti scrivessero anche titolo o data direi che stai
a posto
spero aiuti.
ciao
L.
/**
* Key Generator
*
* @param int Length of the key
* @param string Type of Character
* (lower, upper, numeric, ALPHA, ALNUM)
*
* @return string Generated key from the arguments
*/
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);
}
More information about the italy-tech
mailing list