AbleDesign - Programs
Programs > Random License Generator
<?php
/* Random License Generator
* Version 1.01
* by Dan Kaplan <design@abledesign.com>
* http://AbleDesign.com/ or http://AccountBiller.com/
* Last Modified: January 5, 2009 (PHP5 compatible)
* ------------------------------------------------------------------------
* Random License Generator allows you to create registration numbers of
* any specified length and format, as well as tools for connecting and
* working with a database of existing registration numbers.
*
* USE THIS PROGRAM AT YOUR OWN RISK; no warranties are expressed or
* implied. You may modify the file however you see fit, so long as
* you retain this header information and any credits throughout the file.
* If you make any modifications or improvements, please send them via
* email to Dan Kaplan <design@abledesign.com>.
* ------------------------------------------------------------------------
*/
/*
If you wish to use this in conjunction with a MySQL database for logging
registration numbers and ensuring that the same number is not re-used, use
something like the following table setup:
CREATE TABLE License (
ID int(10) unsigned NOT NULL auto_increment,
License varchar(255) NOT NULL,
PRIMARY KEY (ID),
UNIQUE license_unique_index (License),
KEY license_index (License)
);
$license_table = "License";
$database_name = "DATABASE"; // specify the name of your database
*/
// if you are using the Random License Generator in conjunction with a MySQL
// database for logging the registration numbers, the following function will
// first check that the generated number is not in use and is of a valid
// format. if you use the database method, uncomment the lines in the function
// by removing the forward slashes at the beginning of the line.
function valid_license($license) {
global $database_name, $license_table;
// if (strrpos($license,' ') > 0) {
// return FALSE;
// } else {
// $SQLquery = "SELECT License FROM $license_table WHERE License = '$license'";
// $result = mysql_db_query($database_name, $SQLquery);
// if ($result && mysql_num_rows($result) > 0) {
// return FALSE;
// } else {
return TRUE;
// }
// }
}
if (!isset($_POST['length'])) {
$action = "load_defaults";
$length = "0";
} else {
$length = $_POST['length'];
}
if ($action == "load_defaults") {
// this could be replaced by an included file with format preferences,
// or you could set up multiple "templates" of preference formats here,
// identified by the $tpl variable:
if (isset($tpl) && ($tpl == 1)) {
$length = 9;
$f1 = "0|p";
$f2 = "4|A";
$f3 = "3|A";
$f4 = "3|A";
$f5 = "3|A";
$f6 = "1|A";
$f7 = "1|A";
$f8 = "11|A";
$f9 = "1|A";
} else { // default template
$length = 6;
$f1 = "0|L";
$f2 = "4|A";
$f3 = "3|A";
$f4 = "3|A";
$f5 = "3|A";
$f6 = "1|A";
}
for ($i=1; $i <= $length; $i++) {
$split = explode("|", ${"f".$i});
$_POST["c".$i] = $split[0];
$_POST["sc".$i] = $split[1];
// echo "c".$i." = ". $_POST["c".$i] .", sc".$i." = ". $_POST["sc".$i] ."<br>";
}
} elseif ($action == "set_defaults") {
// for writing preferences to file; not done here, but this would create the
// $f# variables which form the $c# and $sc# variables when loaded. just as
// easy to set your preferences through the "template" section above.
for ($i=1; $i <= $length; $i++) {
// ${"f".$i} = ${"c".$i} ."|". ${"sc".$i};
${"f".$i} = $_POST["c".$i] ."|". $_POST["sc".$i];
// echo "f".$i." = ". ${"f".$i} ."<br>";
}
} elseif ($action == "add") {
// this section is for if you have chosen to use a MySQL database for
// logging the generated license numbers:
$SQLquery = "INSERT INTO $license_table VALUES (NULL, '". addslashes($license) ."')";
$result = mysql_db_query($database_name, $SQLquery);
if (!$result) {
echo("<p>Error Performing Query: ". mysql_error() ."</p>");
exit();
} else {
echo "Registration #$license added to the database";
}
}
$uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$lowercase = "abcdefghijklmnopqrstuvwxyz";
$all_letters = $uppercase.$lowercase;
$numbers = "0123456789";
$letters_and_numbers = $all_letters.$numbers;
$array = array (
"0" => array ("Same Character (Specify at right)", ""),
"1" => array ("All Letters & Numbers", "$letters_and_numbers"),
"2" => array ("All Letters", "$all_letters"),
"3" => array ("Numbers", "$numbers"),
"4" => array ("Hyphen", "--"),
"5" => array ("Underscore", "__"),
"6" => array ("Uppercase Letters", "$uppercase"),
"7" => array ("Lowercase Letters", "$lowercase"),
"8" => array ("Uppercase Vowels", "AEIOUY"),
"9" => array ("Lowercase Vowels", "aeiouy"),
"10" => array ("Uppercase Consonants", "BCDFGHJKLMNPQRSTVWXZ"),
"11" => array ("Lowercase Consonants", "bcdfghjklmnpqrstvwxz")
);
// any other char's you want for the 'Same Character' option can be appended here, i.e. $array[1][1]."*^@!";
$all_string = $array[1][1];
for ($i=0; $i < strlen($all_string); $i++) {
$char = substr($all_string, $i, 1);
$all_array[] = $char;
}
$license = " "; // a # with spaces will not pass the initial valid_license() check, forcing a # to be created
$invalid = TRUE;
while ($invalid) {
$license = "";
mt_srand((double)microtime()*1000000);
for ($i=1; $i <= $length; $i++) {
if ($_POST["c".$i] != 0) {
$string = $array[$_POST["c".$i]][1];
if ($string == "") { $string = $array[1][1]; }
$license .= substr($string, mt_rand(0,strlen($string)-1), 1);
} else {
// means a 'same character' position was selected
$license .= $_POST["sc".$i];
}
}
if (valid_license($license)) {
$invalid = FALSE;
}
}
if ($length == 0) {
$txt = "<p><b>Specify the Length of the desired Registration Number:</b></p>\n\n";
} else {
// you can use a link ($add_link) or button ($add_button) for adding the generated license #
// to the database, assuming you are using a database full of license numbers. just uncomment
// the corresponding lines here and after the for() loop, then place $add_button or $add_link
// where you want it to appear on the page (like next to the displayed license #).
//$add_link = "<a href=\"$PHP_SELF?action=add&license=$license&";
//$add_button = "<form method=\"post\" action=\"$PHP_SELF\">\n";
// combinatorics gone crazy...
$permutations = 1;
for ($i=1; $i <= $length; $i++) {
$this_var = $_POST["c".$i]; // temp variable
if (($this_var == 0) || ($this_var == 4) || ($this_var == 5)) {
// for 4 & 5 (hyphen & underscore), the array contains 2 of each to
// avoid calculation errors on some systems, but there's really only 1
$permutations = $permutations * 1;
} else {
$permutations = $permutations * strlen($array[$this_var][1]);
}
//$add_link .= "c" .$i ."=". $_POST["c".$i] ."&sc" .$i ."=". $_POST["sc".$i] ."&";
//$add_button .= "<input type=\"hidden\" name=\"c".$i."\" value=\"".$_POST["c".$i]."\">\n";
//$add_button .= "<input type=\"hidden\" name=\"sc".$i."\" value=\"".$_POST["sc".$i]."\">\n";
}
//$add_link .= "length=$length\">Add to Database</a>";
//$add_button .= "<input type=\"hidden\" name=\"length\" value=\"$length\">\n";
//$add_button .= "<input type=\"hidden\" name=\"action\" value=\"add\">\n";
//$add_button .= "<input type=\"hidden\" name=\"license\" value=\"$license\">\n";
//$add_button .= "<input type=\"submit\"value=\"Add to Database\"></form>\n";
$txt = "<p><b>Registration Number: <font color=\"#FF0000\">$license</font></b></p>\n";
$txt .= "<p>Number of possible values for this format: <b>". number_format($permutations) ."</b></p>\n\n";
}
$txt .= "<form method=\"post\" action=\"". $_SERVER['PHP_SELF'] ."\">\n";
$txt .= "Length: <input type=\"text\" name=\"length\" value=\"$length\" size=\"15\" maxlength=\"32\"><br><br>\n";
$txt .= "<table cellpadding=\"0\" cellspacing=\"5\" border=\"0\">\n";
// loop through for number of characters in license
for ($i=1; $i <= $length; $i++) {
// character type input
$txt .= "<tr><td valign=\"top\" align=\"right\" nowrap>Character #$i: </td><td valign=\"top\"><select type=\"text\" name=\"c". $i ."\" size=\"1\">\n";
for ($j=0; $j < count($array); $j++) {
$txt .= "<option value=\"$j\"";
if ($_POST["c".$i] == $j) { $txt .= " SELECTED"; }
$txt .= ">". $array[$j][0] ."\n";
}
$txt .= "</select></td><td valign=\"top\">\n";
// specific letter/number input
$txt .= "<select type=\"text\" name=\"sc". $i ."\" size=\"1\">\n";
for ($k=0; $k < count($all_array); $k++) {
$txt .= "<option value=\"". $all_array[$k] ."\"";
if ($_POST["sc".$i] == $all_array[$k]) { $txt .= " SELECTED"; }
$txt .= ">". $all_array[$k] ."\n";
}
$txt .= "</select>\n";
$txt .= "</td></tr>\n";
}
$txt .= "<tr><td colspan=\"3\" align=\"center\">\n";
$txt .= "<input type=\"submit\" value=\"Generate Registration #\"> <input type=\"reset\" value=\"Reset\"></td></tr></table>\n";
$txt .= "</form>\n";
echo $txt;
?>