Article

Creating a Credit Card Validation Class With PHP

Page: 1 2 3 4 5 6 7 Next

Step Two

The values of the numbers that resulted from multiplying every second digit by two are added together (i.e. in our example above, multiplying the 7 by two resulted in 14, which is 1 + 4 = 5). The result of these additions is added to the value of every digit that was not multiplied (i.e. the first digit, the third, the fifth, etc):

5 + (0) + 0 + (0) + 1 + (6) + 6 + (8) + 2 + (4) + 8 + (4) + 8 + (1 + 4) + 3

= 60

Step Three

When a modulus operation is applied to the result of step two, the remainder must equal 0 in order for the number to pass the Mod 10 algorithm. The modulus operator simply returns the remainder of a division, for example:

10 MOD 5 = 0 (5 goes into 10 two times and has a remainder of 0)

20 MOD 6 = 2 (6 goes into 20 three times and has a remainder of 2)

43 MOD 4 = 3 (4 goes into 43 ten times and has a remainder of 3)

So for our test credit card number 378282246310005, we apply a modulus of 10 to the result from step two, like this:

60 MOD 10 = 0

The modulus operation returns 0, indicating that the credit card number is valid.

Now that we understand the Mod 10 algorithm, it's really quite easy to create our own version to validate credit card numbers with PHP. Let's create our credit card class now.

Creating the CCreditCard Class

Let's now create a PHP class that we can use to store and validate the details of a credit card. Our class will be able to hold the cardholder's name, the card type (mastercard, visa, etc), the card number, and the expiry month and date.

Create a new PHP file called class.creditcard.php. As we walk through the following steps, copy-paste each piece of code shown to the file and save it.

We start of by defining several card type constants. These values will be used to represent the type of card that our class will be validating:

<?php  
 
define("CARD_TYPE_MC", 0);  
define("CARD_TYPE_VS", 1);  
define("CARD_TYPE_AX", 2);  
define("CARD_TYPE_DC", 3);  
define("CARD_TYPE_DS", 4);  
define("CARD_TYPE_JC", 5);

Next, we have our class declaration. Our class is called CCreditCard. Note that there is an extra 'C' at the front of the class name intentionally: it's a common programming practice to prefix the name of a class with 'C' to in fact indicate that it is a class.

We also define five member variables, which will be used internally to hold the credit card's name, type, number, expiry month and year:

class CCreditCard  
{  
// Class Members  
var $__ccName = '';  
var $__ccType = '';  
var $__ccNum = '';  
var $__ccExpM = 0;  
var $__ccExpY = 0;

Next we have our class' custom constructor. A constructor is a function that has the same names as the class in which it exists. It returns no value. It is special in the sense that it is automatically executed whenever we create a new instance of that class.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links