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:


Next: Conclusion >>
More PHP Articles
More By David Rusik