[PHP] 信用卡 通用信用卡验证类 大莱卡、发现/罗福斯,JCB、万事达卡和Visa →→→→→进入此内容的聊天室

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

回复 "信用卡 通用信用卡验证类 大莱卡、发现/罗福斯,JCB、万事达卡和Visa"

这儿你可以回复上面这条便签

captcha