Php implementation is used to verify all types of credit card classes _ PHP Tutorial

Source: Internet
Author: User
Php implementation is used to verify all types of credit card classes. Php implementation to verify all types of credit card classes this article mainly introduces php implementation to verify all types of credit card classes, the example analyzes the implementation principle of the credit card class and uses php to verify all types of credit card classes.

This article mainly introduces php implementation for verifying all types of credit card classes. The example analyzes the implementation principle and related usage skills of the credit card class and has some reference value. if you need it, you can refer

This example describes how php is used to verify all types of credit card classes. Share it with you for your reference. The details are as follows:

This php class is relatively complete and can be used to verify different credit cards. it verifies the general rules of credit card numbers and identifies different types of credit cards.
The code accepts a credit card number and a valid date. if both are valid, TRUE is returned; otherwise, FALSE is returned.

This plug-in accepts the following parameters:
$ Number indicates a string of the credit card number.
$ Expiry credit card validity date, in the format of 07/12 or 0712

?

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

// Plug-in 32: Validate Credit Card

// This is an executable example with additional code supplied

// To obtain just the plug-ins please click on the Download link

$ Card = "4567 1234 5678 9101 ";

$ Exp = "06/11 ";

Echo "Validating: $ card: $ exp
";

$ Result = PIPHP_ValidateCC ($ card, $ exp );

If ($ result! = FALSE) echo "Card Validated ";

Else echo "Card did not validate ";

Function PIPHP_ValidateCC ($ number, $ expiry)

{

// Plug-in 32: Validate Credit Card

//

// This plug-in accepts a credit card number and

// An expiry date and returns TRUE or FALSE,

// Depending on whether the details pass date

// And checksum validation. The arguments required

// Are:

//

// $ Number: Credit Card Number

// $ Expiry: Expiry date in the form:

// 07/12 or 0712 (for July, 2012)

$ Number = preg_replace ('/[^ \ d]/', '', $ number );

$ Expiry = preg_replace ('/[^ \ d]/', '', $ expiry );

$ Left = substr ($ number, 0, 4 );

$ Cclen = strlen ($ number );

$ Chksum = 0;

// Diners Club

If ($ left> = 3000) & ($ left <= 3059) |

($ Left> = 3600) & ($ left <= 3699) |

($ Left> = 3800) & ($ left <= 3889 ))

If ($ cclen! = 14) return FALSE;

// JCB

If ($ left> = 3088) & ($ left <= 3094) |

($ Left> = 3096) & ($ left <= 3102) |

($ Left> = 3112) & ($ left <= 3120) |

($ Left> = 3158) & ($ left <= 3159) |

($ Left> = 3337) & ($ left <= 3349) |

($ Left> = 3528) & ($ left <= 3589 ))

If ($ cclen! = 16) return FALSE;

// American Express

Elseif ($ left> = 3400) & ($ left <= 3499) |

($ Left> = 3700) & ($ left <= 3799 ))

If ($ cclen! = 15) return FALSE;

// Carte Blanche

Elseif ($ left> = 3890) & ($ left <= 3899 ))

If ($ cclen! = 14) return FALSE;

// Visa

Elseif ($ left> = 4000) & ($ left <= 4999 ))

If ($ cclen! = 13 & $ cclen! = 16) return FALSE;

// MasterCard

Elseif ($ left> = 5100) & ($ left <= 5599 ))

If ($ cclen! = 16) return FALSE;

// Financialian BankCard

Elseif ($ left == 5610)

If ($ cclen! = 16) return FALSE;

// Discover

Elseif ($ left == 6011)

If ($ cclen! = 16) return FALSE;

// Unknown

Else return FALSE;

For ($ j = 1-($ cclen % 2); $ j <$ cclen; $ j + = 2)

$ Chksum + = substr ($ number, $ j, 1 );

For ($ j = $ cclen % 2; $ j <$ cclen; $ j + = 2)

{

$ D = substr ($ number, $ j, 1) * 2;

$ Chksum + = $ d <10? $ D: $ d-9;

}

If ($ chksum % 10! = 0) return FALSE;

If (mktime (0, 0, 0, substr ($ expiry, 0, 2), date ("t "),

Substr ($ expiry, 2, 2) <time () return FALSE;

Return TRUE;

}

?>

I hope this article will help you with php programming.

This article mainly introduces php implementation for verifying all types of credit card classes. The example analyzes the implementation principle and related usage of credit card classes...

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.