PHP随机生成信用卡卡号的方法
程序员文章站
2022-04-28 09:02:55
本文实例讲述了php随机生成信用卡卡号的方法。分享给大家供大家参考。具体分析如下:
这段php代码根据信用卡卡号产生规则随机生成信用卡卡号,是可以通过验证的,仅供学习参考...
本文实例讲述了php随机生成信用卡卡号的方法。分享给大家供大家参考。具体分析如下:
这段php代码根据信用卡卡号产生规则随机生成信用卡卡号,是可以通过验证的,仅供学习参考,请不要用于非法用途,否则后果自负。
<?php /* php credit card number generator copyright (c) 2006 graham king graham@darkcoding.net this program is free software; you can redistribute it and/or modify it under the terms of the gnu general public license as published by the free software foundation; either version 2 of the license, or (at your option) any later version. this program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. see the gnu general public license for more details. you should have received a copy of the gnu general public license along with this program; if not, write to the free software foundation, inc., 51 franklin street, fifth floor, boston, ma 02110-1301, usa. */ $visaprefixlist[] = "4539"; $visaprefixlist[] = "4556"; $visaprefixlist[] = "4916"; $visaprefixlist[] = "4532"; $visaprefixlist[] = "4929"; $visaprefixlist[] = "40240071"; $visaprefixlist[] = "4485"; $visaprefixlist[] = "4716"; $visaprefixlist[] = "4"; $mastercardprefixlist[] = "51"; $mastercardprefixlist[] = "52"; $mastercardprefixlist[] = "53"; $mastercardprefixlist[] = "54"; $mastercardprefixlist[] = "55"; $amexprefixlist[] = "34"; $amexprefixlist[] = "37"; $discoverprefixlist[] = "6011"; $dinersprefixlist[] = "300"; $dinersprefixlist[] = "301"; $dinersprefixlist[] = "302"; $dinersprefixlist[] = "303"; $dinersprefixlist[] = "36"; $dinersprefixlist[] = "38"; $enrouteprefixlist[] = "2014"; $enrouteprefixlist[] = "2149"; $jcbprefixlist[] = "35"; $voyagerprefixlist[] = "8699"; /* 'prefix' is the start of the cc number as a string, any number of digits. 'length' is the length of the cc number to generate. typically 13 or 16 */ function completed_number($prefix, $length) { $ccnumber = $prefix; # generate digits while ( strlen($ccnumber) < ($length - 1) ) { $ccnumber .= rand(0,9); } # calculate sum $sum = 0; $pos = 0; $reversedccnumber = strrev( $ccnumber ); while ( $pos < $length - 1 ) { $odd = $reversedccnumber[ $pos ] * 2; if ( $odd > 9 ) { $odd -= 9; } $sum += $odd; if ( $pos != ($length - 2) ) { $sum += $reversedccnumber[ $pos +1 ]; } $pos += 2; } # calculate check digit $checkdigit = (( floor($sum/10) + 1) * 10 - $sum) % 10; $ccnumber .= $checkdigit; return $ccnumber; } function credit_card_number($prefixlist, $length, $howmany) { for ($i = 0; $i < $howmany; $i++) { $ccnumber = $prefixlist[ array_rand($prefixlist) ]; $result[] = completed_number($ccnumber, $length); } return $result; } function output($title, $numbers) { $result[] = "<div class='creditcardnumbers'>"; $result[] = "<h3>$title</h3>"; $result[] = implode('<br />', $numbers); $result[]= '</div>'; return implode('<br />', $result); } # # main # echo "<div class='creditcardset'>"; $mastercard = credit_card_number($mastercardprefixlist, 16, 10); echo output("mastercard", $mastercard); $visa16 = credit_card_number($visaprefixlist, 16, 10); echo output("visa 16 digit", $visa16); echo "</div>"; echo "<div class='creditcardset'>"; $visa13 = credit_card_number($visaprefixlist, 13, 5); echo output("visa 13 digit", $visa13); $amex = credit_card_number($amexprefixlist, 15, 5); echo output("american express", $amex); echo "</div>"; # minor cards echo "<div class='creditcardset'>"; $discover = credit_card_number($discoverprefixlist, 16, 3); echo output("discover", $discover); $diners = credit_card_number($dinersprefixlist, 14, 3); echo output("diners club", $diners); echo "</div>"; echo "<div class='creditcardset'>"; $enroute = credit_card_number($enrouteprefixlist, 15, 3); echo output("enroute", $enroute); $jcb = credit_card_number($jcbprefixlist, 16, 3); echo output("jcb", $jcb); echo "</div>"; echo "<div class='creditcardset'>"; $voyager = credit_card_number($voyagerprefixlist, 15, 3); echo output("voyager", $voyager); echo "</div>"; ?>
希望本文所述对大家的php程序设计有所帮助。