A product was developed yesterday and requires that a registration code serial number be generated by the general background of the website. we then generate it in the format of 573-225-374-118, which is unique in the database.
A product was developed yesterday and requires that a registration code/serial number be generated by the general background of the website, and then generated in the format of 573-225-374-118, which is unique in the database, next I will record all the information I found and I wrote myself.
The principle is to generate mt_rand random seed and then use the relevant functions for read operations.
Example 1 code:
- /**
- * Serial number generator
- */
- Function snMaker ($ pre = ''){
- $ Date = date ('ymmd ');
- $ Rand = rand );
- $ Time = mb_substr (time (), 5, 5, 'utf-8 ');
- $ SerialNumber = $ pre. $ date. $ time. $ rand;
- // Echo strlen ($ serialNumber ).' ';
- Return $ serialNumber;
- }
- Echo snMaker ();
- /**
- * Replace a part of a string with a specific character.
- * @ Param str or int $ str string to be processed
- * @ Param str or int $ to replace the string
- * @ Param int $ start keep the first few characters
- * @ Param int $ end keep the last few characters
- */
- Function hideString ($ str = 'hello', $ to = '*', $ start = 1, $ end = 0 ){
- $ Lenth = strlen ($ str)-$ start-$ end;
- $ Lenth = ($ lenth <0 )? 0: $ lenth;
- $ To = str_repeat ($ to, $ lenth );
- $ Str = substr_replace ($ str, $ to, $ start, $ lenth );
- Return $ str;
- }
- Echo hideString ();
Example 2: generate the registration code/serial number. the reference content is as follows:
- /* Configure */
- $ Key_sum = 1500; // maximum number of CD-keys to prevent repeated values
- $ Key_total = 1000; // The final number of CD-keys required
- $ Limiter = "-"; // the connector between each set of numbers in the CD-Key
- $ Save_file = "./cd_key.txt"; // Save the CD-Key File
- $ Num_file = "./number.txt"; // serial number file
- $ File = file ($ num_file); // open the sequence number file
- $ Start_num = 0; // Minimum random number
- $ End_num = count ($ file); // maximum random number
- /* Generate 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 ("rn", "", $ key_str );
- }
- /* Filter duplicate strings and extract the final number of CD-keys 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 ("rn", $ 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 );
- }
- ?>
The execution of the program will generate the cd_key.txt file, which contains a verification code similar to the following, which is referenced below:
573-225-374-118,691-553-280-280,969-594-607-211,251-575-776-563,280--289-739-533...
In this way, we fully achieve our goal. you can also save the above random strings to the database for convenient calling and flexible setting of the above variables, you can generate 16-bit and 20-bit verification codes. if you are interested, you can also write verification codes similar to XDF8F-ADE89-D0J5C-4RTFG.