当前位置:首页 > PHP教程 > php应用 > 列表

PHP随机生成信用卡卡号的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-17 15:52:15 浏览: 评论:0 

这篇文章主要介绍了PHP随机生成信用卡卡号的方法,涉及php根据信用卡卡号规则生成卡号的技巧,具有一定参考借鉴价值,需要的朋友可以参考下。

本文实例讲述了PHP随机生成信用卡卡号的方法,分享给大家供大家参考,具体分析如下:

这段PHP代码根据信用卡卡号产生规则随机生成信用卡卡号,是可以通过验证的,仅供学习参考,请不要用于非法用途,否则后果自负。

  1. <?php 
  2. /* 
  3. PHP credit card number generator 
  4. Copyright (C) 2006 Graham King graham@darkcoding.net 
  5. This program is free software; you can redistribute it and/or 
  6. modify it under the terms of the GNU General Public License 
  7. as published by the Free Software Foundation; either version 2 
  8. of the License, or (at your option) any later version. 
  9. This program is distributed in the hope that it will be useful, 
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of 
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  12. GNU General Public License for more details. 
  13. You should have received a copy of the GNU General Public License 
  14. along with this program; if not, write to the Free Software 
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 
  16. */ 
  17. $visaPrefixList[] = "4539"; 
  18. $visaPrefixList[] = "4556"; 
  19. $visaPrefixList[] = "4916"; 
  20. $visaPrefixList[] = "4532"; 
  21. $visaPrefixList[] = "4929"; 
  22. $visaPrefixList[] = "40240071"; 
  23. $visaPrefixList[] = "4485"; 
  24. $visaPrefixList[] = "4716"; 
  25. $visaPrefixList[] = "4"; 
  26. $mastercardPrefixList[] = "51"; 
  27. $mastercardPrefixList[] = "52"; 
  28. $mastercardPrefixList[] = "53"; 
  29. $mastercardPrefixList[] = "54"; 
  30. $mastercardPrefixList[] = "55"; 
  31. $amexPrefixList[] = "34"; 
  32. $amexPrefixList[] = "37"; 
  33. $discoverPrefixList[] = "6011"; 
  34. $dinersPrefixList[] = "300"; 
  35. $dinersPrefixList[] = "301"; 
  36. $dinersPrefixList[] = "302"; 
  37. $dinersPrefixList[] = "303"; 
  38. $dinersPrefixList[] = "36"; 
  39. $dinersPrefixList[] = "38"; 
  40. $enRoutePrefixList[] = "2014"; 
  41. $enRoutePrefixList[] = "2149"; 
  42. $jcbPrefixList[] = "35"; 
  43. $voyagerPrefixList[] = "8699"; 
  44. /* 
  45. 'prefix' is the start of the CC number as a string, any number of digits. 
  46. 'length' is the length of the CC number to generate. Typically 13 or 16 
  47. */ 
  48. function completed_number($prefix, $length) { 
  49.   $ccnumber = $prefix; 
  50.   # generate digits 
  51.   while ( strlen($ccnumber) < ($length - 1) ) { 
  52.     $ccnumber .= rand(0,9); 
  53.   } 
  54.   # Calculate sum 
  55.   $sum = 0; 
  56.   $pos = 0; 
  57.   $reversedCCnumber = strrev( $ccnumber ); 
  58.   while ( $pos < $length - 1 ) { 
  59.     $odd = $reversedCCnumber[ $pos ] * 2; 
  60.     if ( $odd > 9 ) { 
  61.       $odd -= 9; 
  62.     } 
  63.     $sum += $odd; 
  64.     if ( $pos != ($length - 2) ) { 
  65.       $sum += $reversedCCnumber[ $pos +1 ]; 
  66.     } 
  67.     $pos += 2; 
  68.   } 
  69.   # Calculate check digit 
  70.   $checkdigit = (( floor($sum/10) + 1) * 10 - $sum) % 10; 
  71.   $ccnumber .= $checkdigit; 
  72.   return $ccnumber; 
  73. } 
  74. function credit_card_number($prefixList, $length, $howMany) { 
  75.   for ($i = 0; $i < $howMany; $i++) { 
  76.     $ccnumber = $prefixList[ array_rand($prefixList) ]; 
  77.     $result[] = completed_number($ccnumber, $length); 
  78.   } 
  79.   return $result; 
  80. } 
  81. function output($title, $numbers) { 
  82.   $result[] = "<div class='creditCardNumbers'>"; 
  83.   $result[] = "<h3>$title</h3>"; 
  84.   $result[] = implode('<br />', $numbers); 
  85.   $result[]= '</div>'; 
  86.   return implode('<br />', $result); 
  87. } 
  88. # 
  89. # Main 
  90. # 
  91. echo "<div class='creditCardSet'>"; 
  92. $mastercard = credit_card_number($mastercardPrefixList, 16, 10); 
  93. echo output("Mastercard", $mastercard); 
  94. $visa16 = credit_card_number($visaPrefixList, 16, 10); 
  95. echo output("VISA 16 digit", $visa16); 
  96. echo "</div>"; 
  97. echo "<div class='creditCardSet'>"; 
  98. $visa13 = credit_card_number($visaPrefixList, 13, 5); 
  99. echo output("VISA 13 digit", $visa13); 
  100. $amex = credit_card_number($amexPrefixList, 15, 5); 
  101. echo output("American Express", $amex); 
  102. echo "</div>"; 
  103. # Minor cards 
  104. echo "<div class='creditCardSet'>"; 
  105. $discover = credit_card_number($discoverPrefixList, 16, 3); 
  106. echo output("Discover", $discover); 
  107. $diners = credit_card_number($dinersPrefixList, 14, 3); 
  108. echo output("Diners Club", $diners); 
  109. echo "</div>"; 
  110. echo "<div class='creditCardSet'>"; 
  111. $enRoute = credit_card_number($enRoutePrefixList, 15, 3); 
  112. echo output("enRoute", $enRoute); 
  113. $jcb = credit_card_number($jcbPrefixList, 16, 3); 
  114. echo output("JCB", $jcb); 
  115. echo "</div>"; 
  116. echo "<div class='creditCardSet'>"; 
  117. $voyager = credit_card_number($voyagerPrefixList, 15, 3); 
  118. echo output("Voyager", $voyager); 
  119. echo "</div>"; 
  120. ?>

Tags: PHP随机生成信用卡

分享到: