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数据缓存技术
Feb 14 PHP
将数组写入txt文件 var_export
Apr 21 PHP
php 智能404跳转代码,适合换域名没改变目录的网站
Jun 04 PHP
Memcached常用命令以及使用说明详解
Jun 27 PHP
php中HTTP_REFERER函数用法实例
Nov 21 PHP
php实现读取手机客户端浏览器的类
Jan 09 PHP
浅谈php自定义错误日志
Feb 13 PHP
CentOS安装php v8js教程
Feb 26 PHP
php防止用户重复提交表单
Nov 02 PHP
jQuery+php简单实现全选删除的方法
Nov 28 PHP
PHP微信网页授权的配置文件操作分析
May 29 PHP
使用PHP开发留言板功能
Nov 19 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
加速XP搜索功能堪比vista
2007/03/22 PHP
php-fpm配置详解
2014/02/12 PHP
PHP耦合设计模式实例分析
2018/08/08 PHP
PHP判断访客是否手机端(移动端浏览器)访问的方法总结【4种方法】
2019/03/27 PHP
functional继承模式 摘自javascript:the good parts
2011/06/20 Javascript
jquery插件冲突(jquery.noconflict)解决方法分享
2014/03/20 Javascript
Javascript动态创建div的方法
2015/02/09 Javascript
JS实现简单路由器功能的方法
2015/05/27 Javascript
js实现的鼠标滚轮滚动切换页面效果(类似360默认页面滚动切换效果)
2016/01/27 Javascript
用JavaScript实现让浏览器停止载入页面的方法
2017/01/19 Javascript
jQuery简介_动力节点Java学院整理
2017/07/04 jQuery
ionic 3.0+ 项目搭建运行环境的教程
2017/08/09 Javascript
Javascript快速实现浏览器系统通知
2017/08/26 Javascript
vue2.0实现音乐/视频播放进度条组件
2018/06/06 Javascript
浅谈JS中几种轻松处理'this'指向方式
2019/09/16 Javascript
HTML+JS实现“代码雨”效果源码(黑客帝国文字下落效果)
2020/03/17 Javascript
vue+element 实现商城主题开发的示例代码
2020/03/26 Javascript
[00:35]DOTA2上海特级锦标赛 EG战队宣传片
2016/03/04 DOTA
[43:24]2018DOTA2亚洲邀请赛3月29日 小组赛A组 LGD VS Liquid
2018/03/30 DOTA
[01:15:15]VG VS EG Supermajor小组赛B组胜者组第一轮 BO3第二场 6.2
2018/06/03 DOTA
[48:27]EG vs Liquid 2018国际邀请赛淘汰赛BO3 第二场 8.25
2018/08/29 DOTA
[01:08:30]DOTA2-DPC中国联赛 正赛 Ehome vs Elephant BO3 第一场 2月28日
2021/03/11 DOTA
python代码实现逻辑回归logistic原理
2019/08/07 Python
Python数据分析模块pandas用法详解
2019/09/04 Python
Django admin管理工具TabularInline类用法详解
2020/05/14 Python
python 检测图片是否有马赛克
2020/12/01 Python
澳大利亚设计的优质鞋类和适合澳大利亚生活方式的服装:Rivers
2019/04/23 全球购物
Otiumberg官网:英国半精致珠宝品牌
2021/01/16 全球购物
新员工入职感言
2014/02/01 职场文书
讲文明树新风公益广告宣传方案
2014/02/25 职场文书
《浅水洼里的小鱼》听课反思
2014/02/28 职场文书
大一学生职业生涯规划
2014/03/11 职场文书
2015年业务工作总结范文
2015/04/10 职场文书
运动会表扬稿范文
2015/05/05 职场文书
2019公司管理制度
2019/04/19 职场文书
go select编译期的优化处理逻辑使用场景分析
2021/06/28 Golang