Example of THELUHNMOD-10 of credit card checkpoint algorithm implemented by php

Source: Internet
Author: User
This article mainly introduces the example of php implementation of the credit card check bit algorithm THELUHNMOD-10, friends can refer to the next

This article describes how to implement the luhn mod-10 Algorithm for credit card verification in php. For more information, see

According to The Luhn Mod-10 Method of The payment card check bit algorithm in ISO 2894:

1. Multiply each digit on the card number by the weight. The rule is that if the number of card numbers is an even number, the first digit is multiplied by 2; otherwise, the number is multiplied by 1, and then the numbers are, 1, 2, 1, 2;
2. If each digit is multiplied by 9 after the weight, 9 is subtracted;
3. sum all processed weighted numbers and calculate the modulus using number 10;
4. The remainder must be 0. Otherwise, the input may be incorrect. It may also be a fake number.
With the simple implementation of PHP, front-end verification is better in actual scenarios, such as JS.

The Code is 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 = 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.