Home  >  Article  >  Backend Development  >  Summary of the general verification algorithm for bank card numbers in PHP

Summary of the general verification algorithm for bank card numbers in PHP

藏色散人
藏色散人forward
2021-07-26 15:23:404282browse

Recently due to the company's needs, I wrote some API, which requires some bank card number information. Regarding this verification rule, I found a lot of general verifications, such as number pulls, and later I found that a buddy wrote a relatively detailed introduction to the rules for generating bank cards and made a short summary

LUHN Algorithm

Check Digit Algorithm (Luhn Check Digit Algorithm) is also called The modulus 10 formula is a simple algorithm used to verify the validity of bank card and credit card numbers. Works with credit cards issued by all major credit card companies including American Express, Passport, MasterCard, Discover, and Diners Club. This algorithm was originally formulated by a group of mathematicians in the 1960s. Now the Luhn test numerical algorithm is available to the public and anyone can use it

luhn Algorithm Principle

Suppose you now have a China Merchants Bank card number6225882708965808 (The domestic mainstream bank card number is usually 16 or 19 digits)

Step 1

Starting from the last digit Reverse direction Calculate the sum of the odd digits

For example, calculate the card number above into the next

8 + 8 + 6 + 8 + 7 + 8 + 5 + 2 = 52

steps Two

Start from the last digitReverse direction Multiply the even digit first by 2 If the product is 2 digits then Subtract 9 When summing, if the product is not 2 digits, add directly

For example, calculate the card number above into the following

0 * 2 = 0
5 * 2 = 10 - 9 = 1
9 * 2 = 18 - 9 = 9
0 * 2 = 0
2 * 2 = 4
8 * 2 = 16 - 9 = 7
2 * 2 = 4
6 * 2 = 12 - 9 = 3
最后计算结果
0 + 1 + 9 + 0 + 4 + 7 + 4 + 3 = 28

Step 3

Add the sum of odd digits to the sum of even digits. If the result is divisible by 10, the verification is passed.

52 + 28 = 80
80 % 10 = 0

PHP version of luhn algorithm

    function checkLuhn($card){
        $len=strlen($card);
        $all=[];
        $sum_odd=0;
        $sum_even=0;
        for($i=0;$i<$len;$i++){
            $all[]=substr($card,$len-$i-1,1);
        }
        //all 里的偶数key都是我们要相加的奇数位
        for($k=0;$k<$len;$k++){
            if($k % 2 ==0){
                $sum_odd+=$all[$k];
            }else{
                //奇数key都是要相加的偶数和
                if($all[$k] * 2 >= 10){
                    $sum_even+=$all[$k] * 2 - 9;
                }else{
                    $sum_even+=$all[$k]*2;
                }
            }
        }
        $total=$sum_odd+$sum_even;
        if($total % 10 == 0){
            return true;
        }else{
            return false;
        }
    }

If there are any mistakes, please point them out

Of course, you need to make some judgments before doing this. You can think of your own home. Finally, this is not 100% applicable to all bank card numbers.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Summary of the general verification algorithm for bank card numbers in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete