Article

Creating a Credit Card Validation Class With PHP

Page: 1 2 3 4 5 6 7 Next

We now implement a PHP version of the Mod 10 algorithm, using exactly the same steps that we described earlier:

// Is the number valid?      
$cardNumber = strrev($this->__ccNum);      
$numSum = 0;      
     
for($i = 0; $i < strlen($cardNumber); $i++)      
{      
  $currentNum = substr($cardNumber, $i, 1);      
     
// Double every second digit      
if($i % 2 == 1)      
{      
  $currentNum *= 2;      
}      
     
// Add digits of 2-digit numbers together      
if($currentNum > 9)      
{      
  $firstNum = $currentNum % 10;      
  $secondNum = ($currentNum - $firstNum) / 10;      
  $currentNum = $firstNum + $secondNum;      
}      
     
$numSum += $currentNum;      
}

The $numSum variable will contain the sum of all of the variables from step two of the Mod 10 algorithm, which we described earlier. PHP's symbol for the modulus operator is '%', so we assign true/false to the $passCheck variable, depending on whether or not $numSum has a modulus of zero:

// If the total has no remainder it's OK      
$passCheck = ($numSum % 10 == 0);

If both $validFormat and $passCheck are true, then we return true, to indicate that the card number is valid. If not, we return false, to indicate that either the card number was in an incorrect format, or if failed the Mod 10 check:

  if($validFormat && $passCheck) return true;      
  else return false;      
 }      
}      
?>

And that's all there is to our CCreditCard class! Let's now look at a simple validation example using HTML forms, PHP, and an instance of our CCreditCard class.

Using our CCreditCard Class

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      
if(!isset($submit))      
{      
?>      
     
  <h2>Validate Credit Card</h2>      
  <form name="frmCC" action="testcc.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      
  $cc = new CCreditCard($ccName, $ccType, $ccNum, $ccExpM, $ccExpY);      
     
  ?>      
     
  <h2>Validation Results</h2>      
  <b>Name: </b><?=$cc->Name(); ?><br>      
  <b>Number: </b><?=$cc->SafeNumber('x', 6); ?><br>      
  <b>Type: </b><?=$cc->Type(); ?><br>      
  <b>Expires: </b><?=$cc->ExpiryMonth() . '/' .      
  $cc->ExpiryYear(); ?><br><br>      
     
  <?php      
     
    echo '<font color="blue" size="2"><b>';      
     
    if($cc->IsValid())      
    echo 'VALID CARD';      
    else      
    echo 'INVALID CARD';      
     
    echo '</b></font>';      
  }      
?>

Run the script in your browser and see what happens...

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

Sponsored Links