Generate 1000 random registration codes using PHP

Source: Internet
Author: User
Tags final client

General procedures need to use the registration code, in order to prevent piracy, if the generated registration code to save the database, and through the software in the Client Access server to match the client input of the verification code is correct, this is a good solution to piracy.

The code described below is the use of PHP to generate a digital verification code, similar to: 152-562-986-230, in order to ensure that the verification code is not guessed, we use a more complex 12-digit number, then the possibility is: 999999999999 so much, then the probability of duplication is very small.

Our following program shows an example of a 1000 random registration code. Includes two programs, a program to generate from 100 to 999 of the sequence of numbers and save to the file, the second program is to extract the number of random numbers corresponding to the row of the digital generated verification code string, and then body into 1000 random string, and finally saved in the file.

Program one: Generate sequence numbers

<?php
/**
* File: Generates 4-bit digits
* Author:heiyeluren
* Create:2005-9-5 22:26
*/

/* Configuration * *
$start _num = 100;
$end _num = 999;
$save _file = "./number.txt";

Generate a sequence of numbers from 1000 to 9999
for ($i = $start _num; $i <= $end _num; $i + +)
{
$nums [] = $i;
}

Writes the sequence of numbers generated above to a file
$fp = fopen ($save _file, "w") or Die ("Open $save _file failed");
Fwrite ($FP, implode ("\ r \ n", $nums)) or Die ("Write $save _file failed");
Unset ($nums);
Fclose ($FP);

echo "Create $save _file succeed!";

?>

By executing the above code, a number.txt file is generated in the current program directory, which holds a number similar to the following:

100
101
102
103
104
105
106..


Program two: Generate Verification code


<?php
/**
* File: Generate Cd-key Program
* Author:heiyeluren
* Create:2005-9-5 22:26
*/

 /* configuration */
  $key _sum = 1500;   //cd-key Maximum number to prevent duplicate values
  $key _total = 1000;    //Final required number of Cd-key
  $limiter = "-";   //cd-key the connector between each set of numbers
  $save _file = "./ Cd_key.txt "; Save Cd-key file
  $num _file = "./number.txt";//serial Number file
  $file = file ($num _file); //Open sequence number file
  $start _num = 0;   //minimum random number
  $end _num = count ($file); //MAX random number

 /* generates a random number string */
  $cdkey = Array ();
 for ($i =0 $i < $key _sum $i + +)
 {
     $key _str = $file [Rand_num ($start _num, $ End_num)]. $limiter.
                          $file [Rand_num ($start _num, $end _num)]. $limiter.
                          $file [Rand_num ($start _num, $end _num)]. $limiter.
                          $file [Rand_num ($start _num, $end _num)];
      $cdkey [] = Str_replace ("\ r \ n", "", $key _str);
 }

/* Filter Repeat string and extract the final Cd-key quantity required.
$cdkey = Array_unique ($cdkey);
$key _result = Array ();
for ($i =0; $i < $key _total; $i + +)
{
$key _result[] = $cdkey [$i];
}

/* Write the final cd-key to the file * *
$fp = fopen ($save _file, "w+") or Die ("Open $save _file failed");
Fwrite ($FP, implode ("\ r \ n", $key _result)) or Die ("Write $save _file failed");
Unset ($cdkey);
Unset ($ $key _result);
Fclose ($FP);

echo "Create $key _total key succeed!";

/* Random function * *
function Rand_num ($start, $end)
{
Return Mt_rand ($start, $end);
}

?>

Executing the above program generates the Cd_key.txt file, which contains a captcha similar to the following:

573-225-374-118
691-553-280-280
969-594-607-211
251-575-776-563
280-289-739-533 ...


In this way, to achieve our goal, you can also save the above random string to the database, easy to invoke. Flexibility to set the above variables, you can generate 16-bit, 20-bit authentication code. If you are interested, you can also write a code similar to XDF8F-ADE89-D0J5C-4RTFG, hehe play the imagination.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.