PHP method for randomly generating credit card numbers _ PHP Tutorial

Source: Internet
Author: User
PHP randomly generates a credit card number. PHP: how to generate a random credit card number. This article describes how to generate a random credit card number in PHP. it involves php's skills to generate a card number based on the credit card number rules, you can use PHP to randomly generate a credit card number.

This article describes how to generate a random credit card number in PHP. it involves the skills of php to generate a credit card number based on the credit card number rules. it has some reference value. For more information, see

This example describes how to randomly generate a credit card number using PHP. Share it with you for your reference. The specific analysis is as follows:

This PHP code generates a random credit card number based on the rules generated by the credit card number. it can be verified and is for your reference only. please do not use it for illegal purposes. Otherwise, you will be liable for the consequences.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

/*

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

MERCHANTABILITY or fitness for a particle PURPOSE. See

GNU General Public License for more details.

You shoshould have your ed a copy of the GNU General Public License

Along with this program; if not, write to the Free Software

Foundation, Inc., 51 Franklin Street, th 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 [] ="

";

$ Result [] = "$ title ";

$ Result [] = implode ('
', $ Numbers );

$ Result [] ='

';

Return implode ('
', $ Result );

}

#

# Main

#

Echo"

";

$ 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"

";

Echo"

";

$ 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"

";

# Minor cards

Echo"

";

$ Discover = credit_card_number ($ discoverPrefixList, 16, 3 );

Echo output ("Discover", $ discover );

$ Diners = credit_card_number ($ dinersPrefixList, 14, 3 );

Echo output ("Diners Club", $ diners );

Echo"

";

Echo"

";

$ EnRoute = credit_card_number ($ enRoutePrefixList, 15, 3 );

Echo output ("enRoute", $ enRoute );

$ Jcb = credit_card_number ($ jcbPrefixList, 16, 3 );

Echo output ("JCB", $ jcb );

Echo"

";

Echo"

";

$ Voyager = credit_card_number ($ voyagerPrefixList, 15, 3 );

Echo output ("Voyager", $ voyager );

Echo"

";

?>

I hope this article will help you with php programming.

This article describes how to randomly generate a credit card number using PHP. it involves the skills for php to generate a credit card number based on the credit card number rules...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.