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 中dirname(_file_)讲解
Mar 18 PHP
PHP与MySQL开发中页面乱码的产生与解决
Mar 27 PHP
解析PHP多种序列化与反序列化的方法
Jun 06 PHP
浅析linux下apache服务器的配置和管理
Aug 10 PHP
PHP exif扩展方法开启详解
Jul 28 PHP
php连接odbc数据源并保存与查询数据的方法
Dec 24 PHP
PHP永久登录、记住我功能实现方法和安全做法
Apr 27 PHP
PHP获取当前文件的父目录方法汇总
Jul 21 PHP
php版微信js-sdk支付接口类用法示例
Oct 12 PHP
利用PHP获取网站访客的所在地位置
Jan 18 PHP
PHP基于PDO调用sqlserver存储过程通用方法【基于Yii框架】
Oct 07 PHP
php适配器模式简单应用示例
Oct 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 stream_context_create()作用和用法分析
2011/03/29 PHP
PHP register_shutdown_function函数的深入解析
2013/06/03 PHP
PHP中使用正则表达式提取中文实现笔记
2015/01/20 PHP
PHP采用超长(超大)数字运算防止数字以科学计数法显示的方法
2016/04/01 PHP
PHP PDOStatement::setFetchMode讲解
2019/02/03 PHP
extjs 学习笔记(三) 最基本的grid
2009/10/15 Javascript
jQuery中的常用事件总结
2009/12/27 Javascript
javascript与CSS复习(二)
2010/06/29 Javascript
JQuery中对服务器控件 DropdownList, RadioButtonList, CheckboxList的操作总结
2011/06/28 Javascript
onkeypress字符按键兼容所有浏览器使用介绍
2013/04/24 Javascript
JS清除IE浏览器缓存的方法
2013/07/26 Javascript
利用try-catch判断变量是已声明未声明还是未赋值
2014/03/12 Javascript
jQuery取得设置清空select选择的文本与值
2014/07/08 Javascript
javascript内置对象操作详解
2015/02/04 Javascript
实例代码详解javascript实现窗口抖动及qq窗口抖动
2016/01/04 Javascript
javascript html5 canvas实现可拖动省份的中国地图
2016/03/11 Javascript
js点击按钮实现水波纹效果代码(CSS3和Canves)
2016/09/15 Javascript
微信小程序 动态的设置图片的高度和宽度详解及实例代码
2017/02/24 Javascript
vue绑定的点击事件阻止冒泡的实例
2018/02/08 Javascript
vue 1.0 结合animate.css定义动画效果
2018/07/11 Javascript
解决angular2在双向数据绑定时[(ngModel)]无法使用的问题
2018/09/13 Javascript
VUE2.0 ElementUI2.0表格el-table自适应高度的实现方法
2018/11/28 Javascript
Python中统计函数运行耗时的方法
2015/05/05 Python
Python中内置数据类型list,tuple,dict,set的区别和用法
2015/12/14 Python
python实现杨辉三角思路
2017/07/14 Python
Python实现的朴素贝叶斯分类器示例
2018/01/06 Python
python unittest实现api自动化测试
2018/04/04 Python
Tensorflow卷积神经网络实例
2018/05/24 Python
django反向解析URL和URL命名空间的方法
2018/06/05 Python
使用pandas read_table读取csv文件的方法
2018/07/04 Python
浅谈TensorFlow之稀疏张量表示
2020/06/30 Python
CSS3常用的几种颜色渐变模式总结
2016/11/18 HTML / CSS
大学毕业生通用求职信
2013/09/28 职场文书
工业设计专业推荐信
2013/10/29 职场文书
运动会广播稿200字(10篇)
2014/10/12 职场文书
使用Python获取字典键对应值的方法
2022/04/26 Python