PHP generates unique membership card number

Source: Internet
Author: User
Tags md5 encryption

We will 0-z (0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ) represent the value 0-35, such as the letter Z for 35. So I'm going to get a 5 digit number, the maximum amount of information is 36 of 5 square, 36^5 = 60466176, that is, the largest 5-bit number is equivalent to 10 decimal number: 60466176.

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.

classCode {//Password Dictionaries    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 ', 2 8=> ' S ', 29=> ' T ', 30=> ' U ', 31=> ' V ', 32=> ' W ', 33=> ' X ', 34=> ' Y ', 35=> ' Z '     );  Public functionEncodeid ($int,$format=8) {         $dics=$this-dic; $dnum= 36;//binary number        $arr=Array (); $loop=true;  while($loop) {             $arr[] =$dics[Bcmod($int,$dnum)]; $int=Bcdiv($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 functionDecodeid ($ids) {         $dics=$this-dic; $dnum= 36;//binary number//key value Exchange        $dedic=Array_flip($dics); //go to 0        $id=LTrim($ids,$dics[0]); //reversal        $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  $card _no$code

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 the check code, the city number is already defined, The check code is obtained by a certain algorithm, in this case, we use a simple algorithm: the first three-digit 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, so that the number behind the two-digit check code.

$card _pre = ' 755 '$card _vcsubstr(MD5($card _pre.  $card _no), 0,2$card _vcstrtoupper($card _vcEcho  $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.

Disclaimer: This article is original article, helloweba.com and author have copyright, if need to reprint, please indicate from helloweba.com and keep original link: http://www.helloweba.com/view-blog-259.html

PHP generates unique membership card number

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.