現(xiàn)在大部分的主要信用卡也是用 LUHN 算法來實(shí)現(xiàn), 包括 Visa, Master Card, American Express以及 Discover 等。
LUHN 算法只可以檢測信用卡號碼的合法性,而不會檢測信用卡的其他信息,包括是否過期。
使用方法為:
1. 檢測信用卡號碼是否為 16 位
2. 將信用卡號碼串切割成 16 個(gè)數(shù)字
3. 將切割的數(shù)字由左至右起,每逢單數(shù)位置的數(shù)字乘以 2,如果結(jié)果大于 10,將結(jié)果減 9
4. 將所有乘以 2 位置的數(shù)字的結(jié)果(包括大于 10,將結(jié)果減 9的)相加得到一個(gè)新數(shù)值
5. 將上面求得的數(shù)值求出 10 的余數(shù),如果余數(shù)是 0 表示信用卡號碼正確,否則便是錯(cuò)誤。
1<?php
2
3
9
10
11functionluhn_checker($card_num){
12//將非數(shù)字的字符串移除
13$card_num=preg_replace("/D|s/","",$card_num);
14
15$sum=0;
16for($i=0;$i<strlen($card_num);$i++){
17$digit=substr($card_num,$i,1);
18if(($i%2)==0){
19//將單數(shù)位置的數(shù)值乘以2
20$digit=$digit*2;
21}
22
23if($digit>9)$digit=$digit-9;
24$sum+=$digit;
25}
26
27if(($sum%10)==0&&strlen($card_num)==16){
28returnTRUE;
29}else{
30returnFALSE;
31}
32}
33
34
41?>
MasterCard信用卡測試卡號-creditcard-1
5105105105105100
5111111111111118
5454545454545454
5500000000000004
5555555555551111
5555555555554444
VISA信用卡測試卡號-creditcard-2
4590613013277775
4111111111111111
4226146578860117
4067425543164587
4007000000027
4222222222222
4012888888881881
AmericanExpress信用卡測試卡號-creditcard-3
378282246310005
3111111111111117
343434343434343
370000000000002
340000000000009
371449635398431
378734493671000
Diner’sClub信用卡測試卡號-creditcard-4
38520000023237
30000000000004
30569309025904
Discover信用卡測試卡號-creditcard-5
6011111111111117
6011000000000004
6011000990139424
6011601160116611
6111111111111116
JCB信用卡測試卡號-creditcard-6
3530111333300000
3088000000000009
3566002020360505
Test Credit Card Account Numbers-creditcard-
While testing, use only the credit card numbers listed here. Othernumbers produce an error.
Expiration Datemust be avalid date in the future (usethemmyyformat).
Test Credit Card Account Numbers
| Credit Card Type | Credit Card Number |
| American Express | 378282246310005 |
| American Express | 371449635398431 |
| American Express Corporate | 378734493671000 |
| Australian BankCard | 5610591081018250 |
| Diners Club | 30569309025904 |
| Diners Club | 38520000023237 |
| Discover | 6011111111111117 |
| Discover | 6011000990139424 |
| JCB | 3530111333300000 |
| JCB | 3566002020360505 |
| MasterCard | 5555555555554444 |
| MasterCard | 5105105105105100 |
| Visa | 4111111111111111 |
| Visa | 4012888888881881 |
| Visa | 4222222222222Note:Eventhough this number has a different character count than the othertest numbers, it is the correct and functional number. |
| Processor-specific Cards | |
| Dankort (PBS) | 76009244561 |
| Dankort (PBS) | 5019717010103742 |
| Switch/Solo (Paymentech) | 6331101999990016 |
愛華網(wǎng)



