PHP implementation of the credit card verification bit algorithm The Luhn MOD-10 sample _php instance

Source: Internet
Author: User

According to the algorithm of the payment card check bit in ISO 2894, the Luhn Mod-10 method stipulates:

1, on the card number per digit multiplied by the weight. The rule is that if the number of card numbers is even, the first is multiplied by 2, or multiplied by 1, and then, respectively, 1,2,1,2,1,2;
2, if each digit times the weight after more than 9, then need to subtract 9;
3, all the processed weighted numbers sum, with the number 10 modulo operation;
4, the remainder should be 0, otherwise it may be an input error. It could also be a fake number.
Conveniently PHP simple implementation, the actual scene front-end verification is better, such as JS.

Copy Code code as follows:

function Check_card ($card) {
if (!is_numeric ($card)) return False;
$card _len = strlen ($card);
$i = 0;
$num _i = Array ();
do{
if (! $i) {
$num _x = $card _len% 2? 1:2;
} else {
$num _x = $num _x = = 1? 2:1;
}
$num _i[$i] = (int) $card [$i] * $num _x;
$num _i[$i] = $num _i[$i] > 9? $num _i[$i]-9: $num _i[$i];

}while (Isset ($card [+ + $i]));
$num _sum = array_sum ($num _i);
Return $num _sum% 10? False:true;
}

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.