Generate unique membership card number based on PHP

Source: Internet
Author: User
Tags md5 encryption
This article mainly introduces the generation of unique membership card based on PHP, interested in the reference of friends, I hope to be helpful to everyone.

In the case of not querying the database, each member login will generate a digital letter combination of non-repeating membership card number.

As follows:

In order to do the demonstration, we assume that a club issued a group of 10-digit membership card number, the membership card number is 3 City number + 5-digit card number code + 2-bit check code composition. City number with the area code, such as 755 for Shenzhen, 5-digit card number is 36 of the card number of the system, the following two-bit check code is generated by a certain algorithm, the usefulness of the check code is to verify the legitimacy of the card number. In this case, the 10-bit card number we generate is equivalent to the maximum number of more than 60 million membership cards, and the unique card number is not duplicated.

Php

We use PHP for the binary conversion, 10 binary to 36 binary.

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;  Number of binary $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));  The Public Function Decodeid ($ids) {$dics = $this->dic; $dnum = 36;  Binary number//key value Exchange $dedic = Array_flip ($dics);  Go to 0 $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, define the password dictionary, that is, the value corresponding to the 0-z, the method Encodeid ($int, $format) parameter $int represents a number, $format represents the length of the bits, for example Encodeid (123456789,5) Represents a 36-digit number that converts the number 123456789 to a 5-bit, and the method Decodeid ($ids) is used to convert the 36-decimal number into a 10-binary number.

We can do this to generate the card number:

$code = new Code (); $card _no = $code->encodeid (888888,5);

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

Then, we add the city number and check code, the city number is already defined, check code through a certain algorithm obtained, in this case, we use a simple algorithm: the first three city number and five-digit card number for MD5 encryption, and then take the MD5 value of the first 2 bits as a check code, This gives the two-digit check code that follows the number.

$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 10 decimal numbers from the database, guarantee the unique number, and then combine the above code, resulting in a 10-bit non-repeating membership card number.

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

PHP algorithm for calculating lottery probability

Implementation of the definition and use of Redis cache class in PHP

Php method of receiving mail based on IMAP

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.