>  기사  >  백엔드 개발  >  PHP는 신용카드 번호 규칙에 따라 카드 번호를 생성합니다.

PHP는 신용카드 번호 규칙에 따라 카드 번호를 생성합니다.

墨辰丷
墨辰丷원래의
2018-06-12 10:33:571902검색

이 글에서는 주로 신용카드 번호를 무작위로 생성하는 방법을 소개합니다. 여기에는 신용카드 번호 규칙에 따라 카드번호를 생성하는 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";
/*
&#39;prefix&#39; is the start of the CC number as a string, any number of digits.
&#39;length&#39; 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[] = "<p class=&#39;creditCardNumbers&#39;>";
  $result[] = "<h3>$title</h3>";
  $result[] = implode(&#39;<br />&#39;, $numbers);
  $result[]= &#39;</p>&#39;;
  return implode(&#39;<br />&#39;, $result);
}
#
# Main
#
echo "<p class=&#39;creditCardSet&#39;>";
$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 "</p>";
echo "<p class=&#39;creditCardSet&#39;>";
$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 "</p>";
# Minor cards
echo "<p class=&#39;creditCardSet&#39;>";
$discover = credit_card_number($discoverPrefixList, 16, 3);
echo output("Discover", $discover);
$diners = credit_card_number($dinersPrefixList, 14, 3);
echo output("Diners Club", $diners);
echo "</p>";
echo "<p class=&#39;creditCardSet&#39;>";
$enRoute = credit_card_number($enRoutePrefixList, 15, 3);
echo output("enRoute", $enRoute);
$jcb = credit_card_number($jcbPrefixList, 16, 3);
echo output("JCB", $jcb);
echo "</p>";
echo "<p class=&#39;creditCardSet&#39;>";
$voyager = credit_card_number($voyagerPrefixList, 15, 3);
echo output("Voyager", $voyager);
echo "</p>";
?>

요약: 위 내용은 이 글의 전체 내용입니다. 모든 분들의 공부에 도움이 되었으면 좋겠습니다.

관련 권장 사항:

php 분석 및 문자열 조작 및 태그 클라우드 생성

임의의 밑수를 십진수로 변환하는 PHP 방법

두 배열을 빼는 PHP 방법

위 내용은 PHP는 신용카드 번호 규칙에 따라 카드 번호를 생성합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.