To become familiar with the PHP scripting language.
Note: I purposefully did not accept a credit card number as input through an HTML form then process it on the server side. My motivation was to keep this program simple. I don't presume anyone knows much (if anything) about HTML form processing (using POST or GET) and the Common Gateway Interface (CGI), let alone PHP.
<!DOCTYPE html> <html> <body> <?php function luhn_check ($number) { // Strip any non-digits (useful for credit card numbers with spaces and hyphens) $number = preg_replace('/\D/', '', $number); // Set the string length and parity $number_length = strlen($number); $parity = $number_length % 2; // Loop through each digit and do the maths $total = 0; for ($i = 0; $i < $number_length; $i++) { $digit = $number[$i]; // Multiply alternate digits by two if ($i % 2 == $parity) { $digit*=2; // If the sum is two digits, add them together (in effect) if ($digit > 9) { $digit -= 9; } } // Total up the digits $total += $digit; } // If the total mod 10 equals 0, the number is valid return ($total % 10 == 0) ? TRUE : FALSE; } function determine_cc_issuer ($cc_num) { $issuer = "unknown"; $pattern = "/^([34|37]{2})([0-9]{13})$/"; if (preg_match($pattern,$cc_num)) { $issuer = "American Express"; } else { $pattern = "/^([30|36|38]{2})([0-9]{12})$/"; // Diner's Club if (preg_match($pattern,$cc_num)) { $issuer = "Diner's Club"; } else { $pattern = "/^([6011]{4})([0-9]{12})$/"; // Discover Card if (preg_match($pattern,$cc_num)) { $issuer = "Discover"; } else { $pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/"; // Mastercard if (preg_match($pattern,$cc_num)) { $issuer = "Mastercard"; } else { $pattern = "/^([4]{1})([0-9]{12,15})$/"; // Visa if (preg_match($pattern,$cc_num)) { $issuer = "Visa"; } } } } } return $issuer; } $cc_number = "4045812012381012"; echo "<p>Test credit card number: $cc_number</p>\n"; if (luhn_check($cc_number)) { echo "<p>This credit card number passes the Luhn check.</p>\n"; $pattern = "/^(34|37){1}([0-9]){13}$/"; if (preg_match($pattern,$cc_number)) { echo "<p>Card issuer: American Express</p>\n"; } else { $pattern = "/^3056([0-9]){10}$/"; // Diner's Club if (preg_match($pattern,$cc_number)) { echo "<p>Card issuer: Diner's Club</p>\n"; } else { $pattern = "/^(6011){1}([0-9]){12}$/"; // Discover Card if (preg_match($pattern,$cc_number)) { echo "<p>Card issuer: Discover</p>\n"; } else { $pattern = "/^(51|52|53|54|55){2}([0-9]){12}$/"; // Mastercard if (preg_match($pattern,$cc_number)) { echo "<p>Card issuer: Mastercard</p>\n"; } else { $pattern = "/^(4){1}([0-9]){12,15}$/"; // Visa if (preg_match($pattern,$cc_number)) { echo "<p>Card issuer: Visa</p>\n"; } else { echo "<p>Card issuer: unknown</p>\n"; } } } } } } else { echo "<p>Invalid credit card number.</p>\n"; } ?> </body> </html>
To access validate_cc_number.php on Blue, substitute your user ID in your web browser:
https://blue.cs.sonoma.edu/~userid/validate_cc_number.php
For the following questions, a valid credit card is one that passes the Luhn checksum algorithm.