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 相关文章推荐
php设计模式 Proxy (代理模式)
Jun 26 PHP
php 短链接算法收集与分析
Dec 30 PHP
解析如何修改phpmyadmin中的默认登陆超时时间
Jun 25 PHP
php文件夹与文件目录操作函数介绍
Sep 09 PHP
Php连接及读取和写入mysql数据库的常用代码
Aug 11 PHP
用PHP代码在网页上生成图片
Jul 01 PHP
php实现base64图片上传方式实例代码
Feb 22 PHP
ThinkPHP实现生成和校验验证码功能
Apr 28 PHP
php递归函数怎么用才有效
Feb 24 PHP
Laravel5.5以下版本中如何自定义日志行为详解
Aug 01 PHP
PHP的mysqli_stat()函数讲解
Jan 23 PHP
php学习笔记之字符串常见操作总结
Jul 16 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 str_replace的替换漏洞
2008/03/15 PHP
PHPLog php 程序调试追踪工具
2009/09/09 PHP
PHP CURL模拟GET及POST函数代码
2010/04/25 PHP
PHP 将逗号、空格、回车分隔的字符串转换为数组的函数
2012/06/07 PHP
解析如何屏蔽php中的phpinfo()函数
2013/06/06 PHP
PHP扩展CURL的用法详解
2014/06/20 PHP
PHP也能干大事 随机函数
2015/04/14 PHP
php生成curl命令行的方法
2015/12/14 PHP
yii,CI,yaf框架+smarty模板使用方法
2015/12/29 PHP
非常不错的一个javascript 类
2006/11/07 Javascript
IE6 fixed的完美解决方案
2011/03/31 Javascript
Jquery动态进行图片缩略的原理及实现
2013/08/13 Javascript
JSONP跨域的原理解析及其实现介绍
2014/03/22 Javascript
js实现网页自动刷新可制作节日倒计时效果
2014/05/27 Javascript
JavaScript实现多个重叠层点击切换效果的方法
2015/04/24 Javascript
jQuery获取select选中的option的value值实现方法
2016/08/29 Javascript
jquery实现限制textarea输入字数的方法
2017/09/06 jQuery
vue轮播图插件vue-awesome-swiper
2017/11/27 Javascript
使用Ajax和Jquery配合数据库实现下拉框的二级联动的示例
2018/01/25 jQuery
express+vue+mongodb+session 实现注册登录功能
2018/12/06 Javascript
JSON.stringify()方法讲解
2019/01/31 Javascript
Electron 打包问题:electron-builder 下载各种依赖出错(推荐)
2020/07/09 Javascript
sqlalchemy对象转dict的示例
2014/04/22 Python
Python中apply函数的用法实例教程
2014/07/31 Python
python列表推导式操作解析
2019/11/26 Python
python加密解密库cryptography使用openSSL生成的密匙加密解密
2020/02/11 Python
pytorch加载自己的图像数据集实例
2020/07/07 Python
python之pygame模块实现飞机大战完整代码
2020/11/29 Python
CSS3的first-child选择器实战攻略
2016/04/28 HTML / CSS
html5指南-7.geolocation结合google maps开发一个小的应用
2013/01/07 HTML / CSS
HTML5新增的Css选择器、伪类介绍
2013/08/07 HTML / CSS
美国踏板车和轻便摩托车销售网站:Mega Motor Madness
2020/02/26 全球购物
C#面试题
2016/05/06 面试题
应用电子专业学生的自我评价
2013/10/16 职场文书
中学生检讨书1000字
2014/10/28 职场文书
个人催款函范文
2015/06/23 职场文书