首页 > 开发 > PHP > 正文

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

2024-05-04 23:33:14
字体:
来源:转载
供稿:网友

这篇文章主要介绍了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. # Calculate sum 
  54. $sum = 0; 
  55. $pos = 0; 
  56. $reversedCCnumber = strrev$ccnumber ); 
  57. while ( $pos < $length - 1 ) { 
  58. $odd = $reversedCCnumber$pos ] * 2; 
  59. if ( $odd > 9 ) { 
  60. $odd -= 9; 
  61. $sum += $odd
  62. if ( $pos != ($length - 2) ) { 
  63. $sum += $reversedCCnumber$pos +1 ]; 
  64. $pos += 2; 
  65. # Calculate check digit 
  66. $checkdigit = (( floor($sum/10) + 1) * 10 - $sum) % 10; 
  67. $ccnumber .= $checkdigit
  68. return $ccnumber
  69. function credit_card_number($prefixList$length$howMany) { 
  70. for ($i = 0; $i < $howMany$i++) { 
  71. $ccnumber = $prefixListarray_rand($prefixList) ]; 
  72. $result[] = completed_number($ccnumber$length); 
  73. return $result
  74. function output($title$numbers) { 
  75. $result[] = "<div class='creditCardNumbers'>"
  76. $result[] = "<h3>$title</h3>"
  77. $result[] = implode('<br />'$numbers); 
  78. $result[]= '</div>'
  79. return implode('<br />'$result); 
  80. # Main 
  81. echo "<div class='creditCardSet'>"
  82. $mastercard = credit_card_number($mastercardPrefixList, 16, 10); 
  83. echo output("Mastercard"$mastercard); 
  84. $visa16 = credit_card_number($visaPrefixList, 16, 10); 
  85. echo output("VISA 16 digit"$visa16); 
  86. echo "</div>"
  87. echo "<div class='creditCardSet'>"
  88. $visa13 = credit_card_number($visaPrefixList, 13, 5); 
  89. echo output("VISA 13 digit"$visa13); 
  90. $amex = credit_card_number($amexPrefixList, 15, 5); 
  91. echo output("American Express"$amex); 
  92. echo "</div>"
  93. # Minor cards 
  94. echo "<div class='creditCardSet'>"
  95. $discover = credit_card_number($discoverPrefixList, 16, 3); 
  96. echo output("Discover"$discover); 
  97. $diners = credit_card_number($dinersPrefixList, 14, 3); 
  98. echo output("Diners Club"$diners); 
  99. echo "</div>"
  100. echo "<div class='creditCardSet'>"
  101. $enRoute = credit_card_number($enRoutePrefixList, 15, 3); 
  102. echo output("enRoute"$enRoute); 
  103. $jcb = credit_card_number($jcbPrefixList, 16, 3); 
  104. echo output("JCB"$jcb); 
  105. echo "</div>"
  106. echo "<div class='creditCardSet'>"
  107. $voyager = credit_card_number($voyagerPrefixList, 15, 3); 
  108. echo output("Voyager"$voyager); 
  109. echo "</div>"
  110. ?> 

希望本文所述对大家的php程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表