php credit card validation

Solutions on MaxInterview for php credit card validation by the best coders in the world

showing results for - "php credit card validation"
Amin
24 Sep 2020
1<?
2/* Luhn algorithm number checker - (c) 2005-2008 shaman - www.planzero.org *
3 * This code has been released into the public domain, however please      *
4 * give credit to the original author where possible.                      */
5
6function luhn_check($number) {
7
8  // Strip any non-digits (useful for credit card numbers with spaces and hyphens)
9  $number=preg_replace('/\D/', '', $number);
10
11  // Set the string length and parity
12  $number_length=strlen($number);
13  $parity=$number_length % 2;
14
15  // Loop through each digit and do the maths
16  $total=0;
17  for ($i=0; $i<$number_length; $i++) {
18    $digit=$number[$i];
19    // Multiply alternate digits by two
20    if ($i % 2 == $parity) {
21      $digit*=2;
22      // If the sum is two digits, add them together (in effect)
23      if ($digit > 9) {
24        $digit-=9;
25      }
26    }
27    // Total up the digits
28    $total+=$digit;
29  }
30
31  // If the total mod 10 equals 0, the number is valid
32  return ($total % 10 == 0) ? TRUE : FALSE;
33
34}
35?>
36
Luca
14 May 2019
1$expires =  date_format( \DateTime::createFromFormat('ym', $cc_expiration),"ym");
2
3$now =  date_format(new \DateTime(),"ym");
4
5if ($expires < $now) {
6    return 'Expired!';
7}