Home arrow PHP arrow Page 5 - Creating a Credit Card Validation Class With PHP

Creating a Credit Card Validation Class With PHP

One of the most important functions of any eCommerce web site is credit card number storage and validation. In this article David develops a PHP class that stores the details of a credit card and validates its number using the Mod 10 algorithm.

Author Info:
By: David Rusik
Rating: 5 stars5 stars5 stars5 stars5 stars / 48
February 25, 2002
  1. · Creating a Credit Card Validation Class With PHP
  2. · Credit card validation
  3. · Creating the CCreditCard class
  4. · Creating the CCreditCard class (contd.)
  5. · Using our CCreditCard class
  6. · Conclusion

print this article

Creating a Credit Card Validation Class With PHP - Using our CCreditCard class
(Page 5 of 6 )

Create a new file called testcc.php and save it in the same directory as the class.creditcard.php file. Enter the following code into testcc.php:

<?php include('class.creditcard.php'); ?>


global $submit;




<h2>Validate Credit Card</h2>

<form name="frmCC" action="cctest.php" method="post">

Cardholders name: <input type="text" name="ccName"><br>

Card number: <input type="text" name="ccNum"><br>

Card type: <select name="ccType">

<option value="1">mastercard</option>

<option value="2">Visa</option>

<option value="3">Amex</option>

<option value="4">Diners</option>

<option value="5">Discover</option>

<option value="6">JCB</option>


Expiry Date: <select name="ccExpM">


for($i = 1; $i < 13; $i++)

{ echo '<option>' . $i . '</option>'; }



<select name="ccExpY">


for($i = 2002; $i < 2013; $i++)

{ echo '<option>' . $i . '</option>'; }



<input type="submit" name="submit" value="Validate">






// Check if the card is valid

global $ccName;

global $ccNum;

global $ccType;

global $ccExpM;

global $ccExpY;

$cc = new CCreditCard($ccName, $ccType, $ccNum, $ccExpM, $ccExpY);


<h2>Validation Results</h2>

<b>Name: </b><?php echo $cc->Name(); ?><br>

<b>Number: </b><?php echo $cc->SafeNumber('x', 6); ?><br>

<b>Type: </b><?php echo $cc->Type(); ?><br>

<b>Expires: </b><?php echo $cc->ExpiryMonth() . '/' . $cc->ExpiryYear(); ?><br><br>

<font color="blue" size="2"><b>



echo "VALID CARD";



echo "</b></font>";



Run the script in your browser. Here are two screen shots from my browser. The first one shows the HTML form, and the second shows the output once the form is submitted:

Entering the cards details

The results of the card validation
blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials