note 41926 added to function.rand
| From: | nospam at benwconsulting dot com | Date: | Mon, 26 Apr 2004 19:12:25 +0000 |
| Subject: | note 41926 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-68832@lists.php.net to get a copy of this message | ||
//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
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+41926
Delete -- http://master.php.net/manage/user-notes.php?action=delete+41926&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+41926&report=yes
Search -- http://master.php.net/manage/user-notes.php