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

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

(0)

相关推荐

  • php实现信用卡校验位算法THE LUHN MOD-10示例

    按照ISO 2894中支付卡校验位的算法 The Luhn Mod-10 Method 规定: 1.对卡号上的每位数字乘以权重.其规则是,如果卡号数字个数是偶数,则第一位乘以2,否则就乘以1,然后以后分别是,1,2,1,2,1,2;2.如果每位数字乘以权重后超过9 ,则需要减去 9;3.将所有的处理过的加权数字求和,用 数字 10 求模运算;4.余数应该是0,否则可能是输入错误.也可能是一个假号. 顺手PHP简单实现下,实际场景前端验证好一些,例如JS. 复制代码 代码如下: function

  • php实现通用的信用卡验证类

    本文实例讲述了php实现通用的信用卡验证类.分享给大家供大家参考. 原文说明如下: Credit Card Validation Solution (PHP Edition) Version 3.5 Description Credit Card Validation Solution™ uses a four step process to ensure credit card numbers are keyed in correctly. This procedure accurately

  • PHP实现通过Luhn算法校验信用卡卡号是否有效

    本文实例讲述了PHP实现通过Luhn算法校验信用卡卡号是否有效的方法.分享给大家供大家参考.具体实现方法如下: $numbers = "49927398716 49927398717 1234567812345678 1234567812345670"; foreach (split(' ', $numbers) as $n) echo "$n is ", luhnTest($n) ? 'valid' : 'not valid', '</br>'; fu

  • PHP 芝麻信用接入的注意事项

    芝麻官方下载的SDK,跑不起来,百度搜索一番也没有发现太多的文章 ,只有一个CSDN博客写的一篇文章,比较有参考价值 详细查阅文档+几天测试整理以下几点注意事项: 接入芝麻API接口,应该分2步: 第一步,获得授权,这个,官方的SDK,只要吧参数配置正确,就能操作. 注意下面这些参数,和 芝麻文档对比, 另外最重要的一点,,,芝麻公钥生成,以及修改,生成的公钥按照官方的操作复制进芝麻后台,保存之后,芝麻会生成新的公钥,点击复制,在保存到我们的公钥文件,然后上传到空间给PHP调用. $reques

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

    本文实例讲述了PHP随机生成信用卡卡号的方法.分享给大家供大家参考.具体分析如下: 这段PHP代码根据信用卡卡号产生规则随机生成信用卡卡号,是可以通过验证的,仅供学习参考,请不要用于非法用途,否则后果自负. <?php /* PHP credit card number generator Copyright (C) 2006 Graham King graham@darkcoding.net This program is free software; you can redistribute

  • JavaScript随机生成信用卡卡号的方法

    本文实例讲述了JavaScript随机生成信用卡卡号的方法.分享给大家供大家参考.具体分析如下: 这段JS代码根据信用卡卡号产生规则随机生成信用卡卡号,是可以通过验证的,仅供学习参考,请不要用于非法用途,否则后果自负. var visaPrefixList = new Array( "4539", "4556", "4916", "4532", "4929", "40240071", &

  • Python随机生成信用卡卡号的实现方法

    本文实例讲述了Python随机生成信用卡卡号的实现方法.分享给大家供大家参考.具体分析如下: 这段Python代码根据信用卡卡号产生规则随机生成信用卡卡号,是可以通过验证的,仅供学习参考,请不要用于非法用途,否则后果自负. #!/usr/bin/python """ gencc: A simple program to generate credit card numbers that pass the MOD 10 check (Luhn formula). Usefull

  • Python3通过Luhn算法快速验证信用卡卡号的方法

    本文实例讲述了Python3通过Luhn算法快速验证信用卡卡号的方法.分享给大家供大家参考.具体分析如下: Python3通过Luhn算法快速验证信用卡卡号,python用起来就是爽,很简单的三行代码就可以验证信用卡卡号是否有效 def luhn_check(num): ''' Number - List of reversed digits ''' digits = [int(x) for x in reversed(str(num))] check_sum = sum(digits[::2]

  • Go语言通过Luhn算法验证信用卡卡号是否有效的方法

    本文实例讲述了Go语言通过Luhn算法验证信用卡卡号是否有效的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: package main import (     "fmt"     "strings" ) const input = `49927398716 49927398717 1234567812345678 1234567812345670` var t = [...]int{0, 2, 4, 6, 8, 1, 3, 5, 7, 9}

  • Python OpenCV招商银行信用卡卡号识别的方法

    学在前面 从本篇博客起,我们将实际完成几个小案例,第一个就是银行卡号识别,预计本案例将写 5 篇左右的博客才可以完成,一起加油吧. 本文的目标是最终获取一套招商银行卡,0~9 数字的图,对于下图的数字,我们需要提取出来,便于后续模板匹配使用.不过下图中找到的数字不完整,需要找到尽量多的卡片,然后补齐这些数字. 提取卡片相关数字 先对上文中卡片中的数字进行相关提取操作,加载图片的灰度图,获取目标区域.在画板中模拟一下坐标区域,为了便于进行后续的操作. 具体代码如下: import cv2 as c

  • VB.NET实现验证信用卡卡号

    VB.NET代码验证信用卡卡号是否正确,本代码使用luhn算法验证 Dim creditCardNumber As String creditCardNumber = "1234567891234563" '这里请自行输入你要验证的号码 If creditCardNumber.Length < 16 Then Page.ClientScript.RegisterStartupScript(Me.GetType(), "dd", "alert('错误数

  • js随机生成网页背景颜色的方法

    本文实例讲述了js随机生成网页背景颜色的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: <HTML> <HEAD> <TITLE>随机生成网页背景颜色的JS特效</TITLE> <STYLE> .30pt{font-size:30pt;color:#de3076} </STYLE> <SCRIPT LANGUAGE="JavaScript"> <!-- color=new A

  • python随机生成指定长度密码的方法

    本文实例讲述了python随机生成指定长度密码的方法.分享给大家供大家参考.具体如下: 下面的python代码通过对各种字符进行随机组合生成一个指定长度的随机密码 python中的string对象有几个常用的方法用来输出各种不同的字符: string.ascii_letters 输出ascii码的所有字符 string.digits 输出 '0123456789'. string.punctuation ascii中的标点符号 print string.ascii_letters print s

随机推荐