InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Validating Form Input: Credit Cards in PHP
PHP

Validating Form Input: Credit Cards in PHP

InfinityCoder November 28, 2016

You want to make sure a user hasn’t entered a bogus credit card number.

The is_valid_credit_card() function in Example 9-17 tells you whether a provided credit card number is syntactically valid.
Example 9-17. Validating a credit card number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function is_valid_credit_card($s) {
   // Remove non-digits and reverse
   $s = strrev(preg_replace('/[^\d]/','',$s));
   // compute checksum
   $sum = 0;
   for ($i = 0, $j = strlen($s); $i < $j; $i++) {
      // Use even digits as-is
      if (($i % 2) == 0) {
          $val = $s[$i];
      } else {
          // Double odd digits and subtract 9 if greater than 9
          $val = $s[$i] * 2;
          if ($val > 9) { $val -= 9; }
      }
      $sum += $val;
  }
  // Number is valid if sum is a multiple of ten
  return (($sum % 10) == 0);
}
 
if (! is_valid_credit_card($_POST['credit_card'])) {
    print 'Sorry, that card number is invalid.';
}

Credit cards use the Luhn algorithm to prevent against accidental error. This algorithm, which the is_valid_credit_card() function in Example 9-17 uses, does some manipulations on the individual digits of the card number to tell whether the number is acceptable.

Validating a credit card is a bit like validating an email address. Syntactic validation—  making sure the provided value is a sequence of characters that matches a standard— is relatively easy.

Semantic validation, however, is trickier. The credit card number 4111 1111 1111 1111 sails through the function in Example 9-17 but isn’t valid.

It’s a wellknown test number that looks like a Visa card number. (And, as such, is handy for using in books when one needs an example.)

Just as strong email address validation requires external verification (usually by sending a message to the address with a confirmation link in it), credit card validation requires external validation by submitting the credit card number to a payment processor along with associated account info (cardholder name and address) and making sure you get back an approval.
Syntactic validation is good protection against inadvertent user typos but, obviously, is not all you need to do when checking credit card numbers.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Writing to Standard Output in PHP
You want to write to standard output. Use echo or …

Writing to Standard Output in PHP

Finding the Current Date and Time in PHP
You want to know what the time or date is. …

Finding the Current Date and Time in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap