Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate a unique membership card number by PHP

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to generate a unique membership card number for PHP. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

When we want to number a large amount of data, and the number is limited by digits, such as a 5-digit license plate number, a 10-digit document number, an order serial number, a short URL, and so on, we can use the 36-digit system to calculate non-repetitive numbers that match the number of digits.

We will use 0murz (0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ) to represent the values 0-35, such as the letter Z for 35. In this case, I want to get a 5-digit number, the maximum amount of information is 36 to the fifth power, 36 ^ 5 = 60466176, that is, the largest 5-digit number is equivalent to the decimal number: 60466176.

In this paper, for demonstration, we assume that a club issues a batch of 10-digit membership card numbers, which are composed of 3-digit city number, 5-digit card number code and 2-digit check code. The city code is represented by the area code, for example, 755 represents Shenzhen, the 5-digit card number is composed of 36-digit card number, and the last two check codes are generated by a certain algorithm. the use of the check code is to verify the legitimacy of the card number. In this way, the 10-digit card number we generate is equivalent to a maximum of more than 60 million membership card numbers, and it is the only card number that does not repeat.

PHP

We use PHP for binary conversion, from base 10 to base 36.

The class Code {/ / password dictionary private $dic = array (0 = > '0,1 = >' 1century, 2 = > '2signals, 3 = >' 3levels, 4 = > '4bands, 5 = > 5cycles, 6 = >' 6bands, 7 = > '7bands, 8 = > 8codes, 9 = >' 9bands, 10 = > 'Aids, 11 = >' bundles, 12 = >'C', 13 = > 'Downs, 14 = >' eyed, 15 = > 'Falls, 16 = >' Globe, 17 = > 'Houses, 18 = >' Irecast 19 = > Jacks, 20 = > 'Knights, 21 = >' Lines, 22 = >'M'' 23 = > 'female, 24 = >' oval, 25 = > 'Pure, 26 = >' QQing, 27 = > 'Renewal, 28 = >' Shear, 29 = > 'Thousand, 30 = >' Utility, 31 = >'V', 32 = >'WA', 33 = >'X', 34 = > YY', 35 = >'Z') Public function encodeID ($int, $format=8) {$dics = $this- > dic; $dnum = 36; / / binary 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; / binary number / / key value exchange $dedic = array_flip ($dics); / / Zero $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; } } 我们定义Code类,先定义密码字典,即0-Z分别对应的数值,方法encodeID($int, $format)中参数$int表示数字,$format表示位数长度,比方encodeID(123456789,5)表示将数字123456789转换成5位的36进制编号,而方法decodeID($ids)用于将36进制的编号转换成10进制的编号。 我们可以这样来生成卡号: $code = new Code(); $card_no = $code->

EncodeID (888888 and 5)

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

Then, we add the city number and the check code, the city number is defined, and the check code is obtained through a certain algorithm. in this case, we use a simple algorithm: the first three city numbers and five-bit card numbers are encrypted by md5, and then take the first two digits of MD5 as the check code, so we get the two-digit check code after the number.

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

In practical application, the decimal number can be obtained through the database to ensure that the number is unique, and then the above codes are combined to generate a 10-bit non-repetitive membership card number.

This is the end of the article on "how to generate a unique membership card number for PHP". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report