PHP implements credit card class verification for all types of principles

Source: Internet
Author: User
Tags php class
This article mainly introduces the implementation of PHP to verify all types of credit card class, the case analysis of the credit card class implementation principles and related use skills, with a certain reference value, the need for friends can refer to the next

This example describes the PHP implementation used to validate all types of credit card classes. Share to everyone for your reference. Specific as follows:

This PHP class is more complete, can be used to verify a variety of credit cards, the card number of the common rules for credit card verification, while the different types of credit cards targeted identification.
The code accepts a credit card number and a valid date, and returns True if both are valid, otherwise false.

This plugin accepts the following parameters:
$number A string representing the credit card number
$expiry credit card expiration date in 07/12 or 0712 format

<?php//Plug-in 32:validate credits card//This is a executable example with additional code supplied//to obtain just The Plug-ins click on the Download link$card = "4567 1234 5678 9101"; $exp = "06/11"; echo "Validating: $card: $e Xp<br> "; $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//expiry date and returns TRUE or FALSE,//depending on whether the details pass date//A nd checksum validation. The arguments required//IS:////$number: Credit Card number//$expiry: expiry date in the form://07/12 O  R 0712 (for July,) $number = preg_replace ('/[^\d]/', ' ', $number);  $expiry = preg_replace ('/[^\d]/', ' ', $expiry);  $left = substr ($number, 0, 4);  $cclen = strlen ($number);  $chksum = 0; Diners Club if (($left >=) &&amP    ($left <= 3059) | |    ($left >= 3600) && ($left <= 3699) | |  ($left >= 3800) && ($left <= 3889)) if ($cclen! =) 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! =) return FALSE;      American Express ElseIf (($left >= 3400) && ($left <= 3499) | |  ($left >= 3700) && ($left <= 3799)) if ($cclen! =) return FALSE;  Carte Blanche ElseIf (($left >= 3890) && ($left <= 3899)) if ($cclen! =) return FALSE; Visa ElseIf (($left >= 4000) && ($left <= 4999)) if ($cclen! = && $cclen! =) return Fals  E MasterCard ElseIf ($left >= 5100) && ($left <= 5599))  if ($cclen! =) return FALSE;  Australian Bankcard ElseIf ($left = = 5610) if ($cclen! =) return FALSE;  Discover ElseIf ($left = = 6011) if ($cclen! =) 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%! = 0) return FALSE;  if (mktime (0, 0, 0, substr ($expiry, 0, 2), date ("T"), substr ($expiry, 2, 2)) < time ()) return FALSE; return TRUE;}? >

Summary : The above is the entire content of this article, I hope to be able to help you learn.

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.