How does PHP verify that the entered bank card is valid?

Source: Internet
Author: User
PHP: how to verify if the entered bank card is valid php, code, verification, bank card

How does PHP verify whether the entered bank card is valid? when I enter a bank card for a lost object, I want to verify whether the bank card is valid like an ID card, can verify whether the string matches, please find the complete code! I heard that there is a luhn algorithm, but this seems incomplete. Who will give me a function that can be used right away?


Reply to discussion (solution)

Online, etc ~~~~!! Please

The LUHN algorithm is mainly used to calculate the validity of credit card and other document numbers.
1. starting from the last digit of the card number, multiply the even digit by 2. if the result is two digits, add and save the two digits.
2. add all numbers to get the sum.
3. if the credit card number is valid, the sum can be divided by 10.

Therefore

function luhn($s) {  $n = 0;  for($i=strlen($s)-1; $i>=0; $i--) {    if($i % 2) $n += $s{$i};    else {      $t = $s{$i} * 2;      if($t > 9) $t = $t{0} + $t{1};      $n += $t;    }  }  return ($n % 10) == 0;}

The LUHN algorithm is mainly used to calculate the validity of credit card and other document numbers.
1. starting from the last digit of the card number, multiply the even digit by 2. if the result is two digits, add and save the two digits.
2. add all numbers to get the sum.
3. if the credit card number is valid, the sum can be divided by 10.

Therefore

function luhn($s) {  $n = 0;  for($i=strlen($s)-1; $i>=0; $i--) {    if($i % 2) $n += $s{$i};    else {      $t = $s{$i} * 2;      if($t > 9) $t = $t{0} + $t{1};      $n += $t;    }  }  return ($n % 10) == 0;}

Your code seems to have to check whether the credit card is correct? What about debit cards?

Then go to the algorithm description.

Then go to the algorithm description.
I just tested it. it seems that my credit card cannot be tested either. I cannot test a few cards.

No way. I found the algorithm description online.
If you say no, unless you give an algorithm description that you think is correct

Generally, the last four digits are verified.


The LUHN algorithm is mainly used to calculate the validity of credit card and other document numbers.
1. starting from the last digit of the card number, multiply the even digit by 2. if the result is two digits, add and save the two digits.
2. add all numbers to get the sum.
3. if the credit card number is valid, the sum can be divided by 10.

Therefore

function luhn($s) {  $n = 0;  for($i=strlen($s)-1; $i>=0; $i--) {    if($i % 2) $n += $s{$i};    else {      $t = $s{$i} * 2;      if($t > 9) $t = $t{0} + $t{1};      $n += $t;    }  }  return ($n % 10) == 0;}

Your code seems to have to check whether the credit card is correct? What about debit cards? Do I have to upload a name ?? Never touched



Php credit card number verification function
Http://3aj.cn/php/69.html

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.