PHP implementation of the credit card check bit algorithm the LUHN MOD-10 example _php instance

Source: Internet
Author: User
According to the algorithm of the payment card check digit in ISO 2894 the Luhn Mod-10 method stipulates:

1. Multiply each digit on the card number by the weight. The rule is that if the number of card number is even, then the first bit is multiplied by 2, otherwise it is multiplied by 1, then respectively, 1,2,1,2,1,2;
2, if each digit multiplied by the weight after more than 9, you need to subtract 9;
3. Sum all the weighted numbers processed, and use the number 10 to calculate the modulus;
4, the remainder should be 0, otherwise it may be an input error. It could be a fake number.
Handy PHP Simple implementation, the actual scene front-end verification better, such as JS.

Copy the 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.