Sonoma State University
Department of Computer Science
CS-460: Programming Languages
Exercise 15b

Objective:

To become familiar with the PHP scripting language.

  • I've written a simple PHP program to validate credit card numbers called "validate_cc_number.php"
  • This PHP program computes the checksum on an input credit card number using the Luhn algorithm. It also uses regular expressions to identify the likely card issuer (e.g. VISA, American Express, etc.).
  • Is this program foolproof? No. It cannot detect stolen credit cards. Would this type of program save you significant connection costs in processing fees? Absolutely yes! You pay transaction fees every time you enter a credit or debit card number. A little preprocessing can identify fake cards and save you from paying unnecessary merchant processing fee for bogus card numbers.
  • If you would like to more more about PHP or developing ecommerce websites, please stop by during office hours. I have a LOT of experience building secure ecommerce websites.

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.

validate_cc_number.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>

Installation:

  • On our Blue Unix server (cs.blue.sonoma.edu) create a directory named public_html in your home directory.
  • chmod 755 the public_html directory.
  • Please upload a copy of validate_cc_number.php to your public_html directory inside your home directory on Blue.

How to execute and run the PHP program:

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

Questions:

For the following questions, a valid credit card is one that passes the Luhn checksum algorithm.

  1. Is 378282246310003 a valid credit card number? If a valid credit card number, who is the card issuer (i.e. American Express, Discover, MasterCard, Visa, etc.)?
  2. Is 374245001721009 a valid credit card number? If a valid credit card number, who is the card issuer?
  3. Is 4035874000424977 a valid credit card number? If a valid credit card number, who is the card issuer?
  4. Is 5506920819242668 a valid credit card number? If a valid credit card number, who is the card issuer?
  5. Is 5413330089011640 a valid credit card number? If a valid credit card number, who is the card issuer?
  6. Is 6011000990139424 a valid credit card number? If a valid credit card number, who is the card issuer?
  7. Is 30569309025904 a valid credit card number? If a valid credit card number, who is the card issuer?
  8. Is 5555553753048194 a valid credit card number? If a valid credit card number, who is the card issuer?
  9. Is 6011111111111117 a valid credit card number? If a valid credit card number, who is the card issuer?
  10. Is 4012888888881881 a valid credit card number? If a valid credit card number, who is the card issuer?

Submitting your solution

  • Please submit your answers to the above ten questions to Canvas.
  • Since this is a group assignment, please include the names of each member of your team along with "Exercise 15b" when submitting your solution.