Posted By

quixrick on 12/18/12


Tagged

credit card luhn algorithm fake


Versions (?)

Fake Credit Card Checker


 / Published in: PHP
 

This function checks to see if a credit card number cannot possibly be valid. Note: It does NOT check if a card is valid. A card number can pass this check and still be invalid, but this script will identify card numbers that cannot possibly be valid.

  1. // SET THE CARD NUMBER TO BE TESTED
  2. $card_number = '';
  3.  
  4.  
  5.  
  6. // TEST THE CARD NUMBER
  7. if (is_card_fake($card_number)) {
  8. print "It's Possible This Card Is Valid";
  9. }
  10. else {
  11. print "Card Cannot Possibly Be Valid.";
  12. }
  13.  
  14.  
  15.  
  16. function is_card_fake($card_number) {
  17.  
  18.  
  19.  
  20. $number_sum = 0;
  21.  
  22.  
  23.  
  24.  
  25. // SPLIT THE CARD NUMBER INTO 16 INDIVIDUAL DIGITS
  26. $digits = preg_split('//', $card_number, -1, PREG_SPLIT_NO_EMPTY);
  27.  
  28.  
  29.  
  30.  
  31. // LOOP THROUGH THE DIGITS
  32. for ($i = 0; $i < strlen($card_number); $i++) {
  33.  
  34.  
  35.  
  36. // CHECK TO SEE IF WE SHOULD DOUBLE THE NUMBER OR JUST ADD IT
  37. if ($i % 2 == 0) {
  38.  
  39.  
  40.  
  41. // DOUBLE THE DIGITS
  42. $double_number = $digits[$i] * 2;
  43.  
  44.  
  45.  
  46.  
  47. // IF THE DOUBLED NUMBER IS 10 OR MORE, SPLIT IT APART AND ADD EACH PIECE TO THE SUM
  48. if ($double_number > 9) {
  49. $doubled_digits = preg_split('//', $double_number, -1, PREG_SPLIT_NO_EMPTY);
  50. $number_sum += $doubled_digits[0] + $doubled_digits[1];
  51. }
  52. // OTHERWISE, JUST ADD THE DOUBLED NUMBER TO THE SUM
  53. else {
  54. $number_sum += $double_number;
  55. }
  56.  
  57.  
  58.  
  59. }
  60. // WE DO NOT HAVE TO DOUBLE THESE DIGITS; JUST ADD THEM TO THE SUM
  61. else {
  62. $number_sum += $digits[$i];
  63. }
  64.  
  65.  
  66.  
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73. // CHECK TO SEE IF THE SUM OF THE DIGITS CAN BE DIVIDED BY 10
  74. if ($number_sum % 10 == 0) {
  75. return true;
  76. }
  77. else {
  78. return false;
  79. }
  80.  
  81.  
  82.  
  83. }

Report this snippet  

You need to login to post a comment.