Login

Generating Contract Numbers for Online Licenses using PHP and MySQL

PHP and MySQL form a powerful combination of open source technology available to any web developer. One of the increasing trends in modern web development is the generation of online contracts. E-commerce websites selling services online need automated contract generation for faster delivery of services. This article will help you learn to use this technology so you can get it working on your web site.

One of the most common applications is issuing licenses online. In the real world, licenses are issued on paper and signed by both parties. In the online world, this can be done efficiently with an online contract which can be traced by issuing a contract number. Contract numbers are the best way to track those licenses; with every licensing agreement, there should an accompanying licensing contract.

Simple PHP Contract Generation Script

To understand how to generate contract numbers, let’s consider a basic example:

<?php

echo mt_rand(min, max);

echo ‘<br />';

?>

This piece of random code will generate random number between min and max. So for example, if you need to generate a random number between 10000 and 15000, the code would look like this:

<?php

echo mt_rand(10000, 15000);

echo ‘<br />';

?>

Bear in mind that 10,000 is the minimum random number that will be generated and 15,000 is the maximum random number that will be generated.

There are a countless number of applications for a PHP random number generator script; these may include games, random number quizzes, number-based passwords, etc.

If you’ve learned PHP before, you must have met the PHP random number function rand(). This is the classic random number function. In the simple random number generator script above, we are using:

mt_rand();

The only difference between mt_rand() and rand() is speed. The mt_rand() function is capable of processing four times faster than the old random generator script.

Let’s go deeper and make an attempt to put the generated numbers into the MySQL database. To apply this to a realistic application, let’s write a PHP program that will allow us to enter names in the web form, and then have PHP assign a random number from 1 to 10 associated with that name, and then store the data in the MySQL database. In order to make this happen, we will create a database before we build the PHP script. To create a database using phpmyadmin, take the following steps:

Step 1: Log into phpmyadmin.

Step 2: Click the “database” link.

Step 3: In the create new database area, type: “randomnumber” (this is just an example; in your actual application you can select any name you like).

Step 4: Leave it set to “collation.”

Step 5: Now that we have a database named “randomnumber,” we will create a table. In the “Create new table on database randomnumber” area, let’s type “randgenerated” (again, you can select any names you like).

Step 6: In the number of fields, type “2.” This is 2 because we will store two types of data; the first column will be the name and the second column will be the random number generated.

Step 6: In this stage, phpmyadmin will ask you to enter your desired field name and data types.

For illustration purposes we will use the field “name” and “random,” and then, using the default type varchar, we will use 30 characters for the name and two characters for the associated random number stored.

Leave everything else as it is, and then click save. MySQL will make a table using your defined variables. You can see the screen shot below; your table should be set up to look like that:

{mospagebreak title=Basic Form Code}

Now that the database has been set, we will start to write our form source code and PHP scripts. Let us start with our basic form. We will name this form.htm.

<html>

<head>

<title>Enter your name</title>

</head>

<body>

Enter your name and we will assign a random number from 1 to 10 to be stored in our database.

<br /><br />

<form action="generaterandom.php" method="post">

Name <input type="text" name="user" size="30">

<br />

<input type="submit" value="Submit your name">

</form>

</body>

</html>

After a user presses “Submit your name,” a PHP script named “generaterandom.php” will generate a random number from 1 to 10 and store that number along with the name in the MySQL database.

For the PHP script of generaterandom.php, the following is the flow of information:

<?php

//Step 1: PHP will connect to the database

//Using the information we set above.

//In this illustration, it uses an XAMPP local host

//However this can be applied to any types of Apache based servers running PHP.

$username = "root";

$password = "xxxxx";

$hostname = "localhost";

$database = "randomnumber";

$dbhandle = mysql_connect($hostname, $username, $password)

or die("Unable to connect to MySQL");

$selected = mysql_select_db($database,$dbhandle)

or die("Could not select $database");

//Step 2: PHP will generate a random number from 1 to 10.

//We will incorporate the previous script but we will change the minimum and maximum.

$randomnumber = mt_rand(1,10);

//Step 3: We will extract the name on the form, but first we have to check if the user did not provide a blank or empty data.

What if we would like to have a unique random number? The previous example does not give us unique numbers. In an online licensing solution, contract numbers generated need to be unique to be stored in the database.

So in this case, a new script is needed to execute the following flow:

To implement this, the form.htm will now look as follows:

<html>

<head>

<title>Enter your name</title>

</head>

<body>

Enter your name and we will generate a 4 digit random number to be stored in our database.

<br /><br />

<form action="uniquerandom.php" method="post">

Name <input type="text" name="user" size="30">

<br />

<input type="submit" value="Submit your name">

</form>

</body>

</html>

And for the PHP script named: uniquerandom.php

<?php

//Step 1: PHP will connect to the database

//Using the information we set above.

//In this illustration, it uses an XAMPP local host

//However this can be applied to any types of Apache based servers running PHP.

$username = "root";

$password = "xxxxxxx";

$hostname = "localhost";

$database = "randomnumber";

$dbhandle = mysql_connect($hostname, $username, $password)

or die("Unable to connect to MySQL");

$selected = mysql_select_db($database,$dbhandle)

or die("Could not select $database");

//Step 2: PHP will generate a random number from 1 to 10.

//We will incorporate the previous script but we will change the minimum and maximum.

$randomnumber =mt_rand(1,10);

//Step 3: Query the database if the generated random exist or not, it it exist generate another random number the compare again.

//If the while loop will become false (means that the generated number is now unique), it will exit the loop and proceed to

The above code snippets are just examples. To incorporate them into complex applications, always check the actual data stored in MySQL to make sure it is storing unique values. It is much better to use a very narrow range (like the example above, using 1 to 10 as minimum and maximum values) to test your script to see if it is correct.