PHP
  Home arrow PHP arrow Page 2 - Creating a Credit Card Validation Class Wi...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Creating a Credit Card Validation Class With PHP
By: David Rusik
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 34
    2002-02-25

    Table of Contents:
  • Creating a Credit Card Validation Class With PHP
  • Credit card validation
  • Creating the CCreditCard class
  • Creating the CCreditCard class (contd.)
  • Using our CCreditCard class
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creating a Credit Card Validation Class With PHP - Credit card validation


    (Page 2 of 6 )

    What does we actually mean when we say "validate a credit card number"? Quite simply it means that we run a credit cards number through a special algorithm known as the Mod 10 algorithm.

    This algorithm processes some simple numerical data validation routines against the number and the result of this algorithm can be used to determine whether or not a credit card number is valid. There are several different types of credit cards that one can use to make a purchase, however they can all be validated using the Mod 10 algorithm.

    As well as passing the Mod 10 algorithm, a credit card number must also pass several different formatting rules. A list of these rules for each of the six most popular credit cards is shown below:
    • Mastercard: Must have a prefix of 51 to 55, and must be 16 digits in length.
    • Visa: Must have a prefix of 4, and must be either 13 or 16 digits in length.
    • American Express: Must have a prefix of 34 or 37, and must be 15 digits in length.
    • Diners Club: Must have a prefix of 300 to 305, 36, or 38, and must be 14 digits in length.
    • Discover: Must have a prefix of 6011, and must be 16 digits in length.
    • JCB: Must have a prefix of 3, 1800, or 2131, and must be either 15 or 16 digits in length.
    As mentioned earlier, in this article we will be creating a PHP class that will hold the details of a credit card number and expose a function indicating whether or not the number of that credit card is valid (i.e. whether it passed the Mod 10 algorithm or not). Before we create that class however, let's look at how the Mod 10 algorithm works.

    The Mod 10 algorithm

    There are three steps that the Mod 10 algorithm takes to determine whether or not a credit card number is valid. We will use the valid credit card number 378282246310005 to demonstrate these steps:

    Step One

    The number is reversed and the value of every second digit is doubled, starting with the digit in second place:

    378282246310005

    ... becomes ...

    500013642282873

    and the value of every second digit is doubled:

    5  0 0 0 1 3 6 4 2 2 8 2 8 7 3

       x2    x2    x2     x2    x2    x2    x2

    -------------------------------------------

       0      0     6       8      4     4      14

    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.

    More PHP Articles
    More By David Rusik


     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway