PHP generates a unique membership card number, php implements membership card _ PHP Tutorial

Source: Internet
Author: User
PHP generates a unique membership card number, and php implements membership card. PHP generates a unique membership card number. php generates a unique membership card number without querying the database. Results: PHP generates a unique membership card number, and php implements membership cards.

When you log in without querying the database, each member will generate a membership card number with no duplicate combinations of numbers and letters.

As follows:

When we want to numbers a large number of data, there is a limit on the number of digits, such as a five-digit license plate number, a ten-digit license number, order serial number, and a short URL, we can use the 36-digit system to calculate non-repeated numbers that match the number of digits.

View demo download source code

We use 0-Z (0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ) to represent the value 0-35, for example, the letter Z represents 35. In this case, I want to get a five-digit number. The maximum amount of information is 36 to the power of 5, 36 ^ 5 = 60466176, that is, the maximum number of five digits is equivalent to a 10-digit number: 60466176.

In this article, we assume that a club issues a group of 10 member card numbers. the membership card number consists of three city numbers, five card numbers, and two verification codes. City numbers are represented by area numbers. for example, 755 represents Shenzhen, and 5-digit card numbers are composed of 36-digit card numbers. the last two verification codes are generated by a certain algorithm, the purpose of the verification code is to verify the validity of the card number. In this case, the 10-digit card number we generate is equivalent to a membership card number up to more than 60 million, and is unique and unique.

PHP

We use PHP for hexadecimal conversion, and 10 to 36.

Class Code {// password dictionary private $ dic = array (0 => '0', 1 => '1', 2 => '2 ', 3 => '3', 4 => '4', 5 => '5', 6 => '6', 7 => '7 ', 8 => '8', 9 => '9', 10 => 'A', 11 => 'B', 12 => 'C ', 13 => 'D', 14 => 'e', 15 => 'F', 16 => 'G', 17 => 'h ', 18 => 'I', 19 => 'J', 20 => 'K', 21 => 'L', 22 =>'m ', 23 => 'N', 24 => 'O', 25 => 'P', 26 => 'Q', 27 => 'R ', 28 =>'s ', 29 => 'T', 30 => 'u', 31 => 'V', 32 => 'W ', 33 => 'X', 34 => 'y', 35 => 'Z'); public function encodeID ($ int, $ format = 8) {$ dics = $ this-> dic; $ dnum = 36; // hexadecimal number $ arr = array (); $ loop = true; while ($ loop) {$ arr [] = $ dics [bcmod ($ int, $ dnum)]; $ int = bcp ($ int, $ dnum, 0 ); if ($ int = '0') {$ loop = false ;}} if (count ($ arr) <$ format) $ arr = array_pad ($ arr, $ format, $ dics [0]); return implode ('', array_reverse ($ arr);} public function decodeID ($ ids) {$ dics = $ this-> dic; $ dnum = 36; // hexadecimal number // key-value exchange $ dedic = array_flip ($ dics); // zero removal $ id = ltrim ($ ids, $ dics [0]); // reverse $ id = strrev ($ id); $ v = 0; for ($ I = 0, $ j = strlen ($ id ); $ I <$ j; $ I ++) {$ v = bcadd (bcmul ($ dedic [$ id {$ I}], bcpow ($ dnum, $ I, 0), 0), $ v, 0);} return $ v ;}}

We define the Code class. First, we define the password dictionary, that is, the values corresponding to 0-Z. In the encodeID ($ int, $ format) method, the parameter $ int represents the number, and $ format represents the length of the number of digits, for example, encodeID (123456789,5) indicates to convert the number 123456789 to a 5-digit 36-digit number, and the decodeID ($ ids) method) it is used to convert a 36-digit number into a 10-digit number.

We can generate the card number as follows:

$code = new Code(); $card_no = $code->encodeID(888888,5); 

As shown above, we can get a 5-digit card number, which actually represents the member number whose card number is 888888 (6 8), and the actual conversion is the 5-digit number: 0J1VC.

Next, we add the city number and verification code. The City number is defined, and the verification code is obtained through a certain algorithm. In this example, we use a simple algorithm: perform md5 encryption on the first three city numbers and five card numbers, and then use the first two digits of the md5 value as the verification code. then, the two digits after the md5 value are obtained.

$card_pre = '755'; $card_vc = substr(md5($card_pre.$card_no),0,2); $card_vc = strtoupper($card_vc); echo $card_pre.$card_no.$card_vc; 

In practice, you can get a 10-digit number through the database to ensure that the number is unique. then, combine the above code to generate a 10-digit non-repeated membership card number.

When a member logs in without querying the database, a membership card number with a combination of numbers and letters is generated. Results...

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.