note 41926 deleted from function.rand by aidan
| From: | aidan@php.net | Date: | Sat, 10 Jul 2004 02:32:25 +0000 |
| Subject: | note 41926 deleted from function.rand by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72959@lists.php.net to get a copy of this message | ||
Note Submitter: nospam@benwconsulting.com
----
//Here are two simple functions to create random keys.
//Please note: This will not create valid keys for software
//It's just showing you the use of rand() in conjunction
//with looping.
function randomKey($chars) {
//Microsoft type user keys are not unix savvy
//so the characters are simple uppercase alpha and 1-9
$genChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789';
$retkey = "";
for ($i = 1; $i <= $chars; $i++)
{
$rand = rand(1,strlen($genChars));
$retkey .= substr($genChars,$rand -1,1);
}
return ($retkey);
}
function keyBlock($chars, $blocks)
{
$key = "";
for($i = 0; $i < $blocks;$i++)
{
//Create an array of keys
$key[] = randomKey($chars);
}
$key = implode("-", $key);
return($key);
}
//Use keyBlock(charlength = "", $blocklength = "")
print keyBlock(5,5);
//Will return a variation of (KJA4O-TRYQT-AKR56-IAYFU-2FQBD)
//Having fun with PHP