Home arrow PHP arrow Page 5 - Creating a Credit Card Validation Class With PHP
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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

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'); ?>

<?php

global $submit;



if(!isset($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>

</select><br>

Expiry Date: <select name="ccExpM">

<?php

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

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



?>

</select>

<select name="ccExpY">

<?php

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

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



?>

</select><br><br>

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

</form>

<?

}

else

{

// 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>



<?php



if($cc->IsValid())

echo "VALID CARD";

else

echo "INVALID 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
PHP ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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