php实现通用的信用卡验证类

这篇文章主要介绍了php实现通用的信用卡验证类,涉及信用卡的规则与php字符串操作的相关技巧,具有一定参考借鉴价值,文中有英文原文注释说明,有助于更直观的了解源码相关信息,需要的朋友可以参考下

本文实例讲述了php实现通用的信用卡验证类。分享给大家供大家参考。

原文说明如下:

  1. Credit Card Validation Solution (PHP Edition)
  2. Version 3.5
  3. Description
  4. Credit Card Validation Solution™ uses a four step process to ensure credit card numbers are keyed in correctly. This procedure accurately checks cards from American Express, Australian BankCard, Carte Blache, Diners Club, Discover/Novus, JCB, MasterCard and Visa.
  5. For more information, please read the comments in the code itself.
  6. Installation Instructions
  7. Select the text between the two lines indicated, below.
  8. Copy the text.
  9. Open up a text editor.
  10. Paste the text.
  11. Save that file. When saving it, make sure to:
  12. save it in a directory on your webserver, and
  13. name it with an extension that your server will recognize needs parsing by PHP.
  14. To see it in action, open up that file in your web browswer.

具体代码如下:

  1. <?php
  2. # ------------------------------------------------------------------------
  3. # Credit Card Validation Solution, version 3.5 PHP Edition
  4. # 25 May 2000
  5. #
  6. # COPYRIGHT NOTICE:
  7. # a) This code is property of The Analysis and Solutions Company.
  8. # b) It is being distributed free of charge and on an "as is" basis.
  9. # c) Use of this code, or any part thereof, is contingent upon leaving
  10. # this copyright notice, name and address information in tact.
  11. # d) Written permission must be obtained from us before this code, or any
  12. # part thereof, is sold or used in a product which is sold.
  13. # e) By using this code, you accept full responsibility for its use
  14. # and will not hold the Analysis and Solutions Company, its employees
  15. # or officers liable for damages of any sort.
  16. # f) This code is not to be used for illegal purposes.
  17. # g) Please email us any revisions made to this code.
  18. #
  19. # Copyright 2000 http://www.AnalysisAndSolutions.com/code/
  20. # The Analysis and Solutions Company info@AnalysisAndSolutions.com
  21. # ------------------------------------------------------------------------
  22. #
  23. # DESCRIPTION:
  24. # Credit Card Validation Solution uses a four step process to ensure
  25. # credit card numbers are keyed in correctly. This procedure accurately
  26. # checks cards from American Express, Australian BankCard, Carte Blache,
  27. # Diners Club, Discover/Novus, JCB, MasterCard and Visa.
  28. #
  29. # CAUTION:
  30. # CCVS uses exact number ranges as part of the validation process. These
  31. # ranges are current as of 20 October 1999. If presently undefined ranges
  32. # come into use in the future, this program will improperly deject card
  33. # numbers in such ranges, rendering an error message entitled "Potential
  34. # Card Type Discrepancy." If this happens while entering a card & type
  35. # you KNOW are valid, please contact us so we can update the ranges.
  36. #
  37. # POTENTIAL CUSTOMIZATIONS:
  38. # * If you don't accept some of these card types, edit Step 2, using pound
  39. # signs "#" to comment out the "elseif," "$CardName" and "$ShouldLength"
  40. # lines in question.
  41. # * Additional card types can be added by inserting new "elseif,"
  42. # "$CardName" and "$ShouldLength" lines in Step 2.
  43. # * The three functions here can be called by other PHP documents to check
  44. # any number.
  45. #
  46. # CREDITS:
  47. # We learned of the Mod 10 Algorithm in some Perl code, entitled
  48. # "The Validator," available on Matt's Script Archive,
  49. # http://worldwidemart.com/scripts/readme/ccver.shtml. That code was
  50. # written by David Paris, who based it on material Melvyn Myers reposted
  51. # from an unknown author. Paris credits Aries Solis for tracking down the
  52. # data underlying the algorithm. At the same time, our code bears no
  53. # resemblance to its predecessors. CCValidationSolution was first written
  54. # for Visual Basic, on which Allen Browne and Rico Zschau assisted.
  55. # Neil Fraser helped prune down the OnlyNumericSolution() for Perl.
  56. function CCValidationSolution ($Number) {
  57. global $CardName;
  58. # 1) Get rid of spaces and non-numeric characters.
  59. $Number = OnlyNumericSolution($Number);
  60. # 2) Do the first four digits fit within proper ranges?
  61. # If so, who's the card issuer and how long should the number be?
  62. $NumberLeft = substr($Number, 0, 4);
  63. $NumberLength = strlen($Number);
  64. if ($NumberLeft >= 3000 and $NumberLeft <= 3059) {
  65. $CardName = "Diners Club";
  66. $ShouldLength = 14;
  67. } elseif ($NumberLeft >= 3600 and $NumberLeft <= 3699) {
  68. $CardName = "Diners Club";
  69. $ShouldLength = 14;
  70. } elseif ($NumberLeft >= 3800 and $NumberLeft <= 3889) {
  71. $CardName = "Diners Club";
  72. $ShouldLength = 14;
  73. } elseif ($NumberLeft >= 3400 and $NumberLeft <= 3499) {
  74. $CardName = "American Express";
  75. $ShouldLength = 15;
  76. } elseif ($NumberLeft >= 3700 and $NumberLeft <= 3799) {
  77. $CardName = "American Express";
  78. $ShouldLength = 15;
  79. } elseif ($NumberLeft >= 3528 and $NumberLeft <= 3589) {
  80. $CardName = "JCB";
  81. $ShouldLength = 16;
  82. } elseif ($NumberLeft >= 3890 and $NumberLeft <= 3899) {
  83. $CardName = "Carte Blache";
  84. $ShouldLength = 14;
  85. } elseif ($NumberLeft >= 4000 and $NumberLeft <= 4999) {
  86. $CardName = "Visa";
  87. if ($NumberLength > 14) {
  88. $ShouldLength = 16;
  89. } elseif ($NumberLength < 14) {
  90. $ShouldLength = 13;
  91. } else {
  92. echo "<br /><em>The Visa number entered, $Number, in is 14 digits long.<br />Visa cards usually have 16 digits, though some have 13.<br />Please check the number and try again.</em><br />n";
  93. return FALSE;
  94. }
  95. } elseif ($NumberLeft >= 5100 and $NumberLeft <= 5599) {
  96. $CardName = "MasterCard";
  97. $ShouldLength = 16;
  98. } elseif ($NumberLeft == 5610) {
  99. $CardName = "Australian BankCard";
  100. $ShouldLength = 16;
  101. } elseif ($NumberLeft == 6011) {
  102. $CardName = "Discover/Novus";
  103. $ShouldLength = 16;
  104. } else {
  105. echo "<br /><em>The first 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";
  106. return FALSE;
  107. }
  108. # 3) Is the number the right length?
  109. if ($NumberLength <> $ShouldLength) {
  110. $Missing = $NumberLength - $ShouldLength;
  111. if ($Missing < 0) {
  112. 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";
  113. } else {
  114. echo "<br /><em>The $CardName number entered, $Number, has $Missing too many digit(s).<br />Please check the number and try again.</em><br />n";
  115. }
  116. return FALSE;
  117. }
  118. # 4) Does the number pass the Mod 10 Algorithm Checksum?
  119. if (Mod10Solution($Number) == TRUE) {
  120. return TRUE;
  121. } else {
  122. echo "<br /><em>The $CardName number entered, $Number, is invalid.<br />Please check the number and try again.</em><br />n";
  123. return FALSE;
  124. }
  125. }
  126. function OnlyNumericSolution ($Number) {
  127. # Remove any non numeric characters.
  128. # Ensure number is no more than 19 characters long.
  129. return substr( ereg_replace( "[^0-9]", "", $Number) , 0, 19);
  130. }
  131. function Mod10Solution ($Number) {
  132. $NumberLength = strlen($Number);
  133. $Checksum = 0;
  134. # Add even digits in even length strings
  135. # or odd digits in odd length strings.
  136. for ($Location = 1 - ($NumberLength % 2); $Location < $NumberLength; $Location += 2) {
  137. $Checksum += substr($Number, $Location, 1);
  138. }
  139. # Analyze odd digits in even length strings
  140. # or even digits in odd length strings.
  141. for ($Location = ($NumberLength % 2); $Location < $NumberLength; $Location += 2) {
  142. $Digit = substr($Number, $Location, 1) * 2;
  143. if ($Digit < 10) {
  144. $Checksum += $Digit;
  145. } else {
  146. $Checksum += $Digit - 9;
  147. }
  148. }
  149. # Is the checksum divisible by ten?
  150. return ($Checksum % 10 == 0);
  151. }
  152. # ----------- BEGIN SAMPLE USER INTERFACE SECTION ------------
  153. #
  154. # This section provides a simple sample user interface for the
  155. # Credit Card Validation functions. It generates an HTML form
  156. # where you enter a card number to check.
  157. #
  158. # If a number has been posted by the form, check it.
  159. if ( isset($Number) ) {
  160. # Get rid of spaces and non-numeric characters in posted
  161. # numbers so they display correctly on the input form.
  162. $Number = OnlyNumericSolution($Number);
  163. if (CCValidationSolution($Number) == TRUE) {
  164. echo "<br />The $CardName number entered, $Number, <em>is</em> valid.<br />n";
  165. }
  166. } else {
  167. $Number = "";
  168. }
  169. # Setup an input form. Posting it calls this page again.
  170. echo "<form method="post" action="$REQUEST_URI">n";
  171. echo "<br />Credit Card Number: <input type="text" name="Number" value="$Number">n";
  172. echo "<input type="Submit" name="submitr" value="Check its Validity">n";
  173. echo "</form><br />n";
  174. #
  175. # ------------ END SAMPLE USER INTERFACE SECTION -------------
  176. ?>