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


Posted in PHP onMarch 23, 2015

本文实例讲述了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程序设计有所帮助。

PHP 相关文章推荐
WinXP + Apache +PHP5 + MySQL + phpMyAdmin安装全功略
Jul 09 PHP
使用PHP和XSL stylesheets转换XML文档
Oct 09 PHP
用文本文件制作留言板提示(下)
Oct 09 PHP
MYSQL环境变量设置方法
Jan 15 PHP
php xml文件操作代码(一)
Mar 20 PHP
php 生成静态页面的办法与实现代码详细版
Feb 15 PHP
PHP中英混合字符串截取函数代码
Jul 17 PHP
『PHP』PHP截断函数mb_substr()使用介绍
Apr 22 PHP
php中AES加密解密的例子小结
Feb 18 PHP
php计算税后工资的方法
Jul 28 PHP
简单的自定义php模板引擎
Aug 26 PHP
Thinkphp 框架扩展之数据库驱动常用方法小结
Apr 23 PHP
PHP实现通过Luhn算法校验信用卡卡号是否有效
Mar 23 #PHP
php实现Mongodb自定义方式生成自增ID的方法
Mar 23 #PHP
php实现约瑟夫问题的方法小结
Mar 23 #PHP
php约瑟夫问题解决关于处死犯人的算法
Mar 23 #PHP
PHP贪婪算法解决0-1背包问题实例分析
Mar 23 #PHP
PHP回溯法解决0-1背包问题实例分析
Mar 23 #PHP
PHP动态规划解决0-1背包问题实例分析
Mar 23 #PHP
You might like
php上传图片之时间戳命名(保存路径)
2014/08/15 PHP
PHP实现深度优先搜索算法(DFS,Depth First Search)详解
2017/09/16 PHP
jquery remove方法应用详解
2012/11/22 Javascript
jQuery使用之设置元素样式用法实例
2015/01/19 Javascript
详解AngularJS中的作用域
2015/06/17 Javascript
提高jQuery性能优化的技巧
2015/08/03 Javascript
JavaScript常用正则函数用法示例
2017/01/23 Javascript
浅谈Koa2框架利用CORS完成跨域ajax请求
2018/03/06 Javascript
JS面试题大坑之隐式类型转换实例代码
2018/10/14 Javascript
Javascript实现秒表倒计时功能
2018/11/17 Javascript
在Vant的基础上封装下拉日期控件的代码示例
2018/12/05 Javascript
JavaScript内置对象math,global功能与用法实例分析
2019/06/10 Javascript
vue框架制作购物车小球动画效果实例代码
2019/09/26 Javascript
javascript+css实现进度条效果
2020/03/25 Javascript
jquery实现拖拽小方块效果
2020/12/10 jQuery
[01:14:12]2018DOTA2亚洲邀请赛4.7 总决赛 LGD vs Mineski 第二场
2018/04/09 DOTA
Python数据结构之顺序表的实现代码示例
2017/11/15 Python
有趣的python小程序分享
2017/12/05 Python
浅谈Python黑帽子取代netcat
2018/02/10 Python
浅谈pandas中DataFrame关于显示值省略的解决方法
2018/04/08 Python
flask框架路由常用定义方式总结
2019/07/23 Python
Python测试框架:pytest学习笔记
2020/10/20 Python
python输出国际象棋棋盘的实例分享
2020/11/26 Python
python中lower函数实现方法及用法讲解
2020/12/23 Python
Python基于爬虫实现全网搜索并下载音乐
2021/02/14 Python
HTML5 placeholder(空白提示)属性介绍
2013/08/07 HTML / CSS
叙述DBMS对数据控制功能有哪些
2016/06/12 面试题
介绍一下Linux中的链接
2016/06/05 面试题
毕业生文员求职信
2013/11/03 职场文书
毕业自我鉴定怎么写
2014/03/25 职场文书
人力资源职位说明书
2014/07/29 职场文书
小学家长学校培训材料
2014/08/24 职场文书
物业工程部经理岗位职责
2015/04/09 职场文书
2015年第31个教师节致辞
2015/07/31 职场文书
Python借助with语句实现代码段只执行有限次
2022/03/23 Python
MySQL控制流函数(-if ,elseif,else,case...when)
2022/07/07 MySQL