PHP implementation of common credit card verification class _php skills

Source: Internet
Author: User
Tags html form php programming ranges strlen

The example in this article is about the Universal Credit card verification class that PHP implements. Share to everyone for your reference.

The original text explains as follows:

Credit card Validation Solution (PHP Edition)
Version 3.5

Description
Credit card Validation solution™uses a four the step process to ensure credit card numbers are keyed in correctly. This is procedure accurately checks cards from American Express, Australian Bankcard, Carte Blache, Diners Club, Discover/nov US, JCB, MasterCard and Visa.
For more information, please read the comments in the code itself.

Installation Instructions
Select the text between the two lines indicated, below.
Copy the text.
Open up a-text editor.
Paste the text.
Save that file. When saving it, make sure to:
Save it in directory on your webserver, and
Name it with a extension that your server would recognize needs parsing by PHP.
To the IT in action, the open up this file in your Web Browswer.

The specific code is as follows:

<?php #------------------------------------------------------------------------# credit card Validation Solution , version 3.5 PHP Edition # # # COPYRIGHT NOTICE: # A) This code being property of the analysis and Solut
Ions Company.
# b) It is being distributed free of charge and on ' as is ' basis. # c) Use the This code, or any part thereof, are contingent upon leaving # This copyright notice, name and address Informa
tion in tact. # d) written permission must to obtained from-us before this code, or no # part thereof, are sold or used in a product W
Hich is sold. # e "by using" This code, "accept full responsibility for it use # and would not hold" analysis and Solutions Compa
NY, its employees # or officers liable to damages of any sort.
# f) This is the ' not ' to ' used for illegal purposes.
# g) Please email us no revisions made to this code.   # Copyright http://www.AnalysisAndSolutions.com/code/# The analysis and Solutions company  info@AnalysisAndSolutions.com #------------------------------------------------------------------------# # # # # # # DESCRIPTION: # credit card Validation Solution uses a four step process to ensure # credit card numbers are keyed in Corre ctly. This is procedure accurately # checks cards from American Express, Australian Bankcard, Carte Blache, # diners Club, Discover
/novus, JCB, MasterCard and Visa. # CAUTION: # CCVS uses exact number ranges as part of the validation process. These # ranges are the current as of October 1999. If presently undefined ranges # come into use in the future, this program would improperly deject card # numbers in such RA Nges, rendering an error message entitled "Potential # Card Type discrepancy."
If this happens while entering a card & type # Your KNOW are valid, please contact us so we can update the ranges. # # Potential customizations: # * If you don ' t accept some of this card types, edit step 2, using pound # signs "#" to Co Mment out the "ElseIf," "$CardName"and" $ShouldLength "# lines in question.
# * Additional card types can is added by inserting new ' ElseIf, ' # ' $CardName ' and ' $ShouldLength ' lines in step 2.
# * The three functions here can is called by other PHP documents to check # any number. # Credits: # We learned of the Mod algorithm in some Perl code, entitled # "The Validator," available on Matt ' s Scrip T Archive, # http://worldwidemart.com/scripts/readme/ccver.shtml. That code is # written by David Paris, who based it's material Melvyn Myers reposted # from the unknown author. Paris credits Aries Solis for tracking down the # data underlying the algorithm. At the same time, we code bears no # resemblance to its predecessors.
Ccvalidationsolution was a written # for Visual Basic, on which Allen Browne and Rico Zschau.
# Neil Fraser helped prune down the onlynumericsolution () for Perl.
  function Ccvalidationsolution ($Number) {global $CardName;
  # 1] Get rid of spaces and non-numeric characters. $NumbeR = onlynumericsolution ($Number);
  # 2 Do the ' four digits fit within proper ranges?
  # If So, who's card issuer and how long should the number is?
  $NumberLeft = substr ($Number, 0, 4);
  $NumberLength = strlen ($Number);
    if ($NumberLeft >= 3000 and $NumberLeft <= 3059) {$CardName = "diners Club";
  $ShouldLength = 14;
    } elseif ($NumberLeft >= 3600 and $NumberLeft <= 3699) {$CardName = "diners Club";
  $ShouldLength = 14;
    } elseif ($NumberLeft >= 3800 and $NumberLeft <= 3889) {$CardName = "diners Club";
  $ShouldLength = 14;
    } elseif ($NumberLeft >= 3400 and $NumberLeft <= 3499) {$CardName = "American Express";
  $ShouldLength = 15;
    } elseif ($NumberLeft >= 3700 and $NumberLeft <= 3799) {$CardName = "American Express";
  $ShouldLength = 15;
    } elseif ($NumberLeft >= 3528 and $NumberLeft <= 3589) {$CardName = "JCB";
  $ShouldLength = 16; } elseif ($NumberLeft >= 3890 and $NumberLeFT <= 3899) {$CardName = "Carte Blache";
  $ShouldLength = 14;
    } elseif ($NumberLeft >= 4000 and $NumberLeft <= 4999) {$CardName = "Visa";
    if ($NumberLength >) {$ShouldLength = 16;
    } elseif ($NumberLength <) {$ShouldLength = 13; else {echo "<br/><em>the Visa number entered, $Number, in digits long.<br/>visa u
      Sually have digits, though some have 13.<br-/>please check the number and try Again.</em><br/>n ";
    return FALSE;
    } elseif ($NumberLeft >= 5100 and $NumberLeft <= 5599) {$CardName = "MasterCard";
  $ShouldLength = 16;
    } elseif ($NumberLeft = = 5610) {$CardName = "Australian Bankcard";
  $ShouldLength = 16;
    } elseif ($NumberLeft = = 6011) {$CardName = "discover/novus";
  $ShouldLength = 16; else {echo "<br/><em>the-four digits of the number entered are $NumberLeft. <br/>if that ' s CorrecT, we don ' t accept that type of credit card.<br/>if It's wrong, please try Again.</em><br/>n ";
  return FALSE;
  # 3 is the ' right ' length?
    if ($NumberLength <> $ShouldLength) {$Missing = $NumberLength-$ShouldLength; if ($Missing < 0) {echo "<br/><em>the $CardName number entered, $Number, is Missing". ABS ($Missing).
    "Digit (s). <br/>please Check the number and try Again.</em><br/>n"; else {echo "<br/><em>the $CardName number entered, $Number, has $Missing too many digit (s). <br/& Gt
    Please check the number and try Again.</em><br/>n ";
  return FALSE;
  # 4) Does the number pass the MOD algorithm Checksum?
  if (mod10solution ($Number) = = True) {return true; else {echo "<br/><em>the $CardName number entered, $Number, is invalid.<br/>please check the Nu
  Mber and try Again.</em><br/>n "; RetUrn FALSE;
  The function onlynumericsolution ($Number) {# Remove any non numeric characters.
  # Ensure number is no more than characters long.
Return substr (Ereg_replace ("[^0-9]", "", $Number), 0, 19);
  function Mod10solution ($Number) {$NumberLength = strlen ($Number);
  $Checksum = 0;
  # ADD even digits in even length strings # or odd digits in odd length strings.  for ($Location = 1-($NumberLength% 2); $Location < $NumberLength; $Location + + 2) {$Checksum + + substr ($Number,
  $Location, 1);
  } # Analyze odd digits in even length strings # or even digits in odd length strings. for ($Location = ($NumberLength% 2); $Location < $NumberLength; $Location + + 2) {$Digit = substr ($Number, $Locati
    ON, 1) * 2;
    if ($Digit <) {$Checksum + = $Digit;
    else {$Checksum + = $Digit-9;
  } # is the checksum divisible by ten?
Return ($Checksum% 10 = 0);
#-----------BEGIN SAMPLE USER INTERFACE Section------------## This section provides a simple sample user interface for the ' # Credit card Validation functions.
It generates an HTML form # where and enter a card number to check.
  # If A number has been posted by the form, check it. if (Isset ($Number)) {# Get rid of spaces and non-numeric characters at posted # numbers so they display correct
    Ly on the input form.
    $Number = Onlynumericsolution ($Number); if (ccvalidationsolution ($Number) = = TRUE) {echo "<br/>the $CardName number entered, $Number, &LT;EM&GT;IS&L
    t;/em> valid.<br/>n ";
  } else {$Number = ""; # Setup an input form.
  Posting It calls this page again.
  echo "<form method=" post "action=" $REQUEST _uri ">n";
  echo "&LT;BR/>credit card number: <input type=" text "name=" number "value=" $Number ">n";
  echo "<input type=" Submit "name=" Submitr "value=" Check its validity ">n";
echo "</form><br/>n"; # #------------End SAMPLE USER INTERFACE section-------------? > 

I hope this article will help you with your PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.