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实现MVC开发得最简单的方法――模型
Apr 10 PHP
php SQL Injection with MySQL
Feb 27 PHP
php调用Google translate_tts api实现代码
Aug 07 PHP
php的webservice的wsdl的XML无法显示问题的解决方法
Mar 11 PHP
Windows下的PHP安装文件线程安全和非线程安全的区别
Apr 23 PHP
php中通过DirectoryIterator删除整个目录的方法
Mar 13 PHP
php正则匹配文章中的远程图片地址并下载图片至本地
Sep 29 PHP
PHP Curl模拟登录微信公众平台、新浪微博实例代码
Jan 28 PHP
php mongodb操作类 带几个简单的例子
Aug 25 PHP
Laravel中的Blade模板引擎示例详解
Oct 10 PHP
实例讲解PHP中使用命名空间
Jan 27 PHP
基于PHP+Mysql简单实现了图书购物车系统的实例详解
Aug 06 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之PHP语法学习笔记1
2006/12/17 PHP
PHP 中检查或过滤IP地址的实现代码
2011/11/27 PHP
PHP实现链式操作的原理详解
2016/09/16 PHP
一个非常实用的php文件上传类
2017/07/04 PHP
如何通过View::first使用Laravel Blade的动态模板详解
2017/09/21 PHP
PHP实现从PostgreSQL数据库检索数据分页显示及根据条件查找数据示例
2018/06/09 PHP
PDO::exec讲解
2019/01/28 PHP
学习YUI.Ext 第六天--关于树TreePanel(Part 1)
2007/03/10 Javascript
node.js操作mysql(增删改查)
2015/07/24 Javascript
jQuery过滤HTML标签并高亮显示关键字的方法
2015/08/07 Javascript
超详细的javascript数组方法汇总
2015/11/21 Javascript
js获取图片宽高的方法
2015/11/25 Javascript
js实现将选中内容分享到新浪或腾讯微博
2015/12/16 Javascript
easyui validatebox验证
2016/04/29 Javascript
RequireJS使用注意细节
2016/05/15 Javascript
Javascript实现跑马灯效果的简单实例
2016/05/31 Javascript
JavaScript实战(原生range和自定义特效)简单实例
2016/08/21 Javascript
js与jQuery实现的用户注册协议倒计时功能实例【三种方法】
2017/11/09 jQuery
自定义Vue组件打包、发布到npm及使用教程
2019/05/22 Javascript
Vue配置marked链接添加target=&quot;_blank&quot;的方法
2019/07/19 Javascript
vue+mock.js实现前后端分离
2019/07/24 Javascript
详解基于Vue的支持数据双向绑定的select组件
2019/09/02 Javascript
js实现秒表计时器
2019/12/16 Javascript
Python函数嵌套实例
2014/09/23 Python
R vs. Python 数据分析中谁与争锋?
2017/10/18 Python
rabbitmq(中间消息代理)在python中的使用详解
2017/12/14 Python
Python实现对文件进行单词划分并去重排序操作示例
2018/07/10 Python
浅析python中numpy包中的argsort函数的使用
2018/08/30 Python
Python 实现一行输入多个数字(用空格隔开)
2020/04/29 Python
购买一个高级域名:BuyDomains
2018/03/11 全球购物
英国婴儿产品专家:Samuel Johnston
2020/04/20 全球购物
会计大学生职业生涯规划书范文
2014/01/13 职场文书
项目负责人任命书
2014/06/04 职场文书
七年级生物教学反思
2016/02/20 职场文书
2016年度先进班组事迹材料
2016/03/01 职场文书
Python opencv缺陷检测的实现及问题解决
2021/04/24 Python