AbleDesign - Support Forum

AbleDesign Forum Index AbleDesign

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
AbleDesign Forum Index
General Discussion
random lisence generator
Post new topic   Reply to topic

Post new topic   Reply to topic    AbleDesign Forum Index -> General Discussion
View previous topic :: View next topic  
Author Message
swapnil_009
Lurker
Lurker


Joined: 07 Sep 2009
Posts: 3

PostPosted: Mon Sep 07, 2009 5:25 am    Post subject: random lisence generator Reply with quote

i just use this script but not able to use it with mysql nothing is added to tabels pls help me out here is the code i used

Code:
<?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 = "de";    // 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;

?>
Back to top
View user's profile Send private message
Dan
Admin / Developer
Admin / Developer


Joined: 19 Feb 2001
Posts: 2057

PostPosted: Mon Sep 07, 2009 10:33 am    Post subject: Reply with quote

You need to add some code to the top of the page to connect to MySQL, and your $license_table and $database_name variables need to be moved outside the /* */ comments section.

Cheers,
Dan
Back to top
View user's profile Send private message Send e-mail Visit poster's website
swapnil_009
Lurker
Lurker


Joined: 07 Sep 2009
Posts: 3

PostPosted: Mon Sep 07, 2009 11:18 am    Post subject: thanx for ur reply Reply with quote

Code:
<?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)
);
*/



// 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.


$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = "de";

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);

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 $dbname VALUES (NULL, '". addslashes($license) ."')";
    $result = mysql_db_query($dbname, $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";
$add_link .= "length=$length\">Add to Database</a>";
echo $txt;

?>


this is my new code but it still dont connect to db and the add button/link dont appers any where.

Any help ...........
Back to top
View user's profile Send private message
Dan
Admin / Developer
Admin / Developer


Joined: 19 Feb 2001
Posts: 2057

PostPosted: Mon Sep 07, 2009 11:26 am    Post subject: Reply with quote

Did you leave the password blank or fill it in in your version?

Also, you will have to do some work to tie the license generator into MySQL. What's provided is just some very starter example code, as that is an unsupported feature of the free script.

Cheers,
Dan
Back to top
View user's profile Send private message Send e-mail Visit poster's website
swapnil_009
Lurker
Lurker


Joined: 07 Sep 2009
Posts: 3

PostPosted: Mon Sep 07, 2009 11:45 am    Post subject: Reply with quote

i am running it on my local machine so i dont use pass for db.

ya i know this is what i got in free .

thanx for this great free script.

Cheers Mate
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AbleDesign Forum Index -> General Discussion All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group


Home | Services | Pricing | Portfolio | About Us | Contact
Support | Programs | Purchase | Tutorials | Site Map

© 1999-2018 AbleDesign.com - Web Design that Can!