PHP registration code/serial number generation instance program

Source: Internet
Author: User
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:

  1. /**
  2. * Serial number generator
  3. */
  4. Function snMaker ($ pre = ''){
  5. $ Date = date ('ymmd ');
  6. $ Rand = rand );
  7. $ Time = mb_substr (time (), 5, 5, 'utf-8 ');
  8. $ SerialNumber = $ pre. $ date. $ time. $ rand;
  9. // Echo strlen ($ serialNumber ).' ';
  10. Return $ serialNumber;
  11. }
  12. Echo snMaker ();
  13. /**
  14. * Replace a part of a string with a specific character.
  15. * @ Param str or int $ str string to be processed
  16. * @ Param str or int $ to replace the string
  17. * @ Param int $ start keep the first few characters
  18. * @ Param int $ end keep the last few characters
  19. */
  20. Function hideString ($ str = 'hello', $ to = '*', $ start = 1, $ end = 0 ){
  21. $ Lenth = strlen ($ str)-$ start-$ end;
  22. $ Lenth = ($ lenth <0 )? 0: $ lenth;
  23. $ To = str_repeat ($ to, $ lenth );
  24. $ Str = substr_replace ($ str, $ to, $ start, $ lenth );
  25. Return $ str;
  26. }
  27. Echo hideString ();

Example 2: generate the registration code/serial number. the reference content is as follows:

  1. /* Configure */
  2. $ Key_sum = 1500; // maximum number of CD-keys to prevent repeated values
  3. $ Key_total = 1000; // The final number of CD-keys required
  4. $ Limiter = "-"; // the connector between each set of numbers in the CD-Key
  5. $ Save_file = "./cd_key.txt"; // Save the CD-Key File
  6. $ Num_file = "./number.txt"; // serial number file
  7. $ File = file ($ num_file); // open the sequence number file
  8. $ Start_num = 0; // Minimum random number
  9. $ End_num = count ($ file); // maximum random number
  10. /* Generate a random number string */
  11. $ Cdkey = array ();
  12. For ($ I = 0; $ I <$ key_sum; $ I ++)
  13. {
  14. $ Key_str = $ file [rand_num ($ start_num, $ end_num)]. $ limiter.
  15. $ File [rand_num ($ start_num, $ end_num)]. $ limiter.
  16. $ File [rand_num ($ start_num, $ end_num)]. $ limiter.
  17. $ File [rand_num ($ start_num, $ end_num)];
  18. $ Cdkey [] = str_replace ("rn", "", $ key_str );
  19. }
  20. /* Filter duplicate strings and extract the final number of CD-keys required */
  21. $ Cdkey = array_unique ($ cdkey );
  22. $ Key_result = array ();
  23. For ($ I = 0; $ I <$ key_total; $ I ++)
  24. {
  25. $ Key_result [] = $ cdkey [$ I];
  26. }
  27. /* Write the final CD-Key to the file */
  28. $ Fp = fopen ($ save_file, "w +") or die ("Open $ save_file failed ");
  29. Fwrite ($ fp, implode ("rn", $ key_result) or die ("Write $ save_file failed ");
  30. Unset ($ cdkey );
  31. Unset ($ key_result );
  32. Fclose ($ fp );
  33. Echo "Create $ key_total key succeed! ";
  34. /* Random function */
  35. Function rand_num ($ start, $ end)
  36. {
  37. Return mt_rand ($ start, $ end );
  38. }
  39. ?>

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.

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.