Random ID Strings
Author : DigitaLink
Works best as an include file. The functions will return a string of random characters of any specified length. You choose upper&lowercase letters and numbers, numbers only, upper&lowercase letters only, or lowercase letters only. Also includes a function to create anti-robot images if you have a directory with images in it. (named a.gif, b.gif, etc.) You need upper and lowercase images and number images. Very handy for user sign-ups!
| PHP Example : | | <?php
function assign_rand_value($num, $alphanums) {
// accepts 1 - 62 (1-52 alpha, 53-62 numeric)
$rand_value = $alphanums[$num];
return $rand_value;
}
function get_rand_id($length) { // ID is letters and numbers
$alphanum = 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", "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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
if($length>0) {
$rand_id="";
for($i=1; $i<=$length; $i++) {
mt_srand((double)microtime() * 1000000);
$num = mt_rand(1,62);
$rand_id .= assign_rand_value($num, $alphanum);
}
}
return $rand_id;
}
function get_rand_numbers($length) { // ID is numbers only
$alphanum = 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", "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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
if($length>0) {
$rand_id="";
for($i=1; $i<=$length; $i++) {
mt_srand((double)microtime() * 1000000);
$num = mt_rand(53,62);
$rand_id .= assign_rand_value($num, $alphanum);
}
}
return $rand_id;
}
function get_rand_letters($length) {
$alphanum = 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", "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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
if($length>0) {
$rand_id="";
for($i=1; $i<=$length; $i++) {
mt_srand((double)microtime() * 1000000);
$num = mt_rand(1,52);
$rand_id .= assign_rand_value($num, $alphanum);
}
}
return $rand_id;
}
function get_rand_lower($length) {
$alphanum = 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", "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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
if($length>0) {
$rand_id="";
for($i=1; $i<=$length; $i++) {
mt_srand((double)microtime() * 1000000);
$num = mt_rand(1,26);
$rand_id .= assign_rand_value($num, $alphanum);
}
}
return $rand_id;
}
function anti_bot_image($key) {
$size = strlen($key);
for ($i=0; $i<$size; $i++) { // put each letter in an array
$keyArray[$i] = $key{$i};
}
foreach ($keyArray as $imgLetter) { // get each letter's image in array
$imgArray[] = "<img src='";
$imgArray[] = $DOCUMENT_ROOT."/images/".$imgLetter.".gif";
$imgArray[] = "' title='";
$imgArray[] = $imgLetter."'>";
}
$displayCode = implode("", $imgArray);
return $displayCode;
}
?>
| |