网络实验室

 找回密码
 注册账户
查看: 1585|回复: 1

Random License Generator

[复制链接]
无心的棋子 发表于 2007-8-8 04:50:43 | 显示全部楼层 |阅读模式
  1. <?php

  2. /* Random License Generator
  3. * Version 1.0
  4. * by Dan Kaplan <[email][email protected][/email]>
  5. *        [url]http://AbleDesign.com/[/url] or [url]http://AccountBiller.com/[/url]
  6. * Last Modified: June 2, 2001
  7. * ------------------------------------------------------------------------
  8. * Random License Generator allows you to create registration numbers of
  9. * any specified length and format, as well as tools for connecting and
  10. * working with a database of existing registration numbers.
  11. *
  12. * USE THIS PROGRAM AT YOUR OWN RISK; no warranties are expressed or
  13. * implied. You may modify the file however you see fit, so long as
  14. * you retain this header information and any credits throughout the file.
  15. * If you make any modifications or improvements, please send them via
  16. * email to Dan Kaplan <[email][email protected][/email]>.
  17. * ------------------------------------------------------------------------
  18. */




  19. /*
  20. If you wish to use this in conjunction with a MySQL database for logging
  21. registration numbers and ensuring that the same number is not re-used, use
  22. something like the following table setup:

  23. CREATE TABLE License (
  24.    ID int(10) unsigned NOT NULL auto_increment,
  25.    License varchar(255) NOT NULL,
  26.    PRIMARY KEY (ID),
  27.    UNIQUE license_unique_index (License),
  28.    KEY license_index (License)
  29. );

  30. $license_table = "License";
  31. $database_name = "DATABASE";    // specify the name of your database
  32. */



  33. // if you are using the Random License Generator in conjunction with a MySQL
  34. // database for logging the registration numbers, the following function will
  35. // first check that the generated number is not in use and is of a valid
  36. // format.  if you use the database method, uncomment the lines in the function
  37. // by removing the forward slashes at the beginning of the line.
  38. function valid_license($license) {
  39.     global $database_name, $license_table;

  40. //    if (strrpos($license,' ') > 0) {
  41. //        return FALSE;
  42. //    } else {
  43. //        $SQLquery = "SELECT License FROM $license_table WHERE License = '$license'";
  44. //        $result = mysql_db_query($database_name, $SQLquery);
  45. //        if ($result && mysql_num_rows($result) > 0) {
  46. //            return FALSE;
  47. //        } else {
  48.             return TRUE;
  49. //        }
  50. //    }
  51. }


  52. if (!isset($length)) {
  53.     $action = "load_defaults";
  54. }

  55. if ($action == "load_defaults") {
  56.     // this could be replaced by an included file with format preferences,
  57.     // or you could set up multiple "templates" of preference formats here,
  58.     // identified by the $tpl variable:

  59.     if ($tpl == 1) {
  60.         $length = 9;
  61.         $f1 = "0|p";
  62.         $f2 = "4|A";
  63.         $f3 = "3|A";
  64.         $f4 = "3|A";
  65.         $f5 = "3|A";
  66.         $f6 = "1|A";
  67.         $f7 = "1|A";
  68.         $f8 = "11|A";
  69.         $f9 = "1|A";
  70.     } else {    // default template
  71.         $length = 6;
  72.         $f1 = "0|L";
  73.         $f2 = "4|A";
  74.         $f3 = "3|A";
  75.         $f4 = "3|A";
  76.         $f5 = "3|A";
  77.         $f6 = "1|A";
  78.     }

  79.     for ($i=1; $i <= $length; $i++) {
  80.         $split = explode("|", ${"f".$i});
  81.         ${"c".$i} = $split[0];
  82.         ${"sc".$i} = $split[1];

  83.         // echo "c".$i." = ". ${"c".$i} .", sc".$i." = ". ${"sc".$i} ."<br>";
  84.     }

  85. } elseif ($action == "set_defaults") {
  86.     // for writing preferences to file; not done here, but this would create the
  87.     // $f# variables which form the $c# and $sc# variables when loaded.  just as
  88.     // easy to set your preferences through the "template" section above.

  89.     for ($i=1; $i <= $length; $i++) {

  90.         ${"f".$i} = ${"c".$i} ."|". ${"sc".$i};
  91.         // echo "f".$i." = ". ${"f".$i} ."<br>";

  92.     }

  93. } elseif ($action == "add") {
  94.     // this section is for if you have chosen to use a MySQL database for
  95.     // logging the generated license numbers:

  96.     $SQLquery = "INSERT INTO $license_table VALUES (NULL, '$license')";
  97.     $result = mysql_db_query($database_name, $SQLquery);
  98.     if (!$result) {
  99.         echo("<p>Error Performing Query: ". mysql_error() ."</p>");
  100.         exit();
  101.     } else {
  102.         echo "Registration #$license added to the database";
  103.     }
  104. }


  105. $uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  106. $lowercase = "abcdefghijklmnopqrstuvwxyz";
  107. $all_letters = $uppercase.$lowercase;
  108. $numbers = "0123456789";
  109. $letters_and_numbers = $all_letters.$numbers;

  110. $array = array (
  111.   "0"  => array ("Same Character (Specify at right)", ""),
  112.   "1"  => array ("All Letters & Numbers",             "$letters_and_numbers"),
  113.   "2"  => array ("All Letters",                       "$all_letters"),
  114.   "3"  => array ("Numbers",                           "$numbers"),
  115.   "4"  => array ("Hyphen",                            "--"),
  116.   "5"  => array ("Underscore",                        "__"),
  117.   "6"  => array ("Uppercase Letters",                 "$uppercase"),
  118.   "7"  => array ("Lowercase Letters",                 "$lowercase"),
  119.   "8"  => array ("Uppercase Vowels",                  "AEIOUY"),
  120.   "9"  => array ("Lowercase Vowels",                  "aeiouy"),
  121.   "10" => array ("Uppercase Consonants",              "BCDFGHJKLMNPQRSTVWXZ"),
  122.   "11" => array ("Lowercase Consonants",              "bcdfghjklmnpqrstvwxz")
  123. );

  124. // any other char's you want for the 'Same Character' option can be appended here, i.e. $array[1][1]."*^@!";
  125. $all_string = $array[1][1];
  126. for ($i=0; $i < strlen($all_string); $i++) {
  127.     $char = substr($all_string, $i, 1);
  128.     $all_array[] = $char;
  129. }


  130. $license = " ";    // a # with spaces will not pass the initial valid_license() check, forcing a # to be created
  131. $invalid = TRUE;

  132. while ($invalid) {
  133.     $license = "";
  134.     mt_srand((double)microtime()*1000000);

  135.     for ($i=1; $i <= $length; $i++) {
  136.         if (${"c".$i} != 0) {
  137.             $string = $array[${"c".$i}][1];
  138.             if ($string == "") { $string = $array[1][1]; }
  139.             $license .= substr($string, mt_rand(0,strlen($string)-1), 1);
  140.         } else {
  141.             // means a 'same character' position was selected
  142.             $license .= ${"sc".$i};
  143.         }
  144.     }

  145.     if (valid_license($license)) {
  146.         $invalid = FALSE;
  147.     }
  148. }



  149. if ($length == 0) {
  150.     $txt = "<p><b>Specify the Length of the desired Registration Number:</b></p>\n\n";
  151. } else {

  152.     // you can use a link ($add_link) or button ($add_button) for adding the generated license #
  153.     // to the database, assuming you are using a database full of license numbers.  just uncomment
  154.     // the corresponding lines here and after the for() loop, then place $add_button or $add_link
  155.     // where you want it to appear on the page (like next to the displayed license #).
  156.     //$add_link = "<a href="$PHP_SELF?action=add&license=$license&";
  157.     //$add_button = "<form method="post" action="$PHP_SELF">\n";

  158.     // combinatorics gone crazy...
  159.     $permutations = 1;
  160.     for ($i=1; $i <= $length; $i++) {
  161.         $this = ${"c".$i};    // temp variable
  162.         if (($this == 0) || ($this == 4) || ($this == 5)) {
  163.             // for 4 & 5 (hyphen & underscore), the array contains 2 of each to
  164.             // avoid calculation errors on some systems, but there's really only 1
  165.             $permutations = $permutations * 1;
  166.         } else {
  167.             $permutations = $permutations * strlen($array[$this][1]);
  168.         }

  169.         //$add_link .= "c" .$i ."=". ${"c".$i} ."&sc" .$i ."=". ${"sc".$i} ."&";
  170.         //$add_button .= "<input type="hidden" name="c".$i."" value="".${"c".$i}."">\n";
  171.         //$add_button .= "<input type="hidden" name="sc".$i."" value="".${"sc".$i}."">\n";
  172.     }

  173.     //$add_link .= "length=$length">Add to Database</a>";
  174.     //$add_button .= "<input type="hidden" name="length" value="$length">\n";
  175.     //$add_button .= "<input type="hidden" name="action" value="add">\n";
  176.     //$add_button .= "<input type="hidden" name="license" value="$license">\n";
  177.     //$add_button .= "<input type="submit"value="Add to Database"></form>\n";

  178.     $txt = "<p><b>Registration Number: <font color="#FF0000">$license</font></b></p>\n";
  179.     $txt .= "<p>Number of possible values for this format: <b>". number_format($permutations) ."</b></p>\n\n";
  180. }


  181. $txt .= "<form method="post" action="$PHP_SELF">\n";
  182. $txt .= "Length: <input type="text" name="length" value="$length" size="15" maxlength="32"><br><br>\n";
  183. $txt .= "<table cellpadding="0" cellspacing="5" border="0">\n";

  184. for ($i=1; $i <= $length; $i++) {
  185.     $txt .= "<tr><td valign="top" align="right" nowrap>Character #$i: </td>";
  186.     $txt .= "<td valign="top"><select type="text" name="c". $i ."" size="1">\n";

  187.     for ($j=0; $j < count($array); $j++) {
  188.         $txt .= "<option value="$j"";
  189.             if (${"c".$i} == $j) { $txt .= " SELECTED"; }
  190.         $txt .= ">". $array[$j][0] ."\n";
  191.     }
  192.     $txt .= "</select></td><td valign="top">\n";

  193.     $txt .= "<select type="text" name="sc". $i ."" size="1">\n";
  194.     for ($k=0; $k < count($all_array); $k++) {
  195.         $txt .= "<option value="". $all_array[$k] .""";
  196.             if (${"sc".$i} == $all_array[$k]) { $txt .= " SELECTED"; }
  197.         $txt .= ">". $all_array[$k] ."\n";
  198.     }
  199.     $txt .= "</select>\n";
  200.    
  201.     $txt .= "</td></tr>\n";
  202. }

  203. $txt .= "<tr><td colspan="3" align="center">\n";
  204. $txt .= "<input type="submit" value="Generate Registration #"> ";
  205. $txt .= "<input type="reset" value="Reset"></td></tr></table>\n";
  206. $txt .= "</form>\n";

  207. echo $txt;

  208. ?>
复制代码
bloghk 发表于 2007-8-8 23:43:26 | 显示全部楼层
看看看看看看看看看看
您需要登录后才可以回帖 登录 | 注册账户

本版积分规则

黑屋|存档|手机|网络实验室 本站服务器由美国合租以及IDCLayer国际数据提供!!!

GMT+8, 2024-4-29 18:39 , Processed in 0.083354 second(s), 11 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表