/ 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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// SET THE CARD NUMBER TO BE TESTED $card_number = ''; // TEST THE CARD NUMBER if (is_card_fake($card_number)) { print "It's Possible This Card Is Valid"; } else { print "Card Cannot Possibly Be Valid."; } function is_card_fake($card_number) { $number_sum = 0; // SPLIT THE CARD NUMBER INTO 16 INDIVIDUAL DIGITS // LOOP THROUGH THE DIGITS // CHECK TO SEE IF WE SHOULD DOUBLE THE NUMBER OR JUST ADD IT if ($i % 2 == 0) { // DOUBLE THE DIGITS $double_number = $digits[$i] * 2; // IF THE DOUBLED NUMBER IS 10 OR MORE, SPLIT IT APART AND ADD EACH PIECE TO THE SUM if ($double_number > 9) { $number_sum += $doubled_digits[0] + $doubled_digits[1]; } // OTHERWISE, JUST ADD THE DOUBLED NUMBER TO THE SUM else { $number_sum += $double_number; } } // WE DO NOT HAVE TO DOUBLE THESE DIGITS; JUST ADD THEM TO THE SUM else { $number_sum += $digits[$i]; } } // CHECK TO SEE IF THE SUM OF THE DIGITS CAN BE DIVIDED BY 10 if ($number_sum % 10 == 0) { return true; } else { return false; } }