Skip to content

Instantly share code, notes, and snippets.

@muhittin
Created January 9, 2012 11:45
Show Gist options
  • Save muhittin/1582613 to your computer and use it in GitHub Desktop.
Save muhittin/1582613 to your computer and use it in GitHub Desktop.
Credit Card Verification VIA LUHN
/* Credit Card Verification VIA LUHN by serpico
function card_verification($ccn){
$ccn = str_replace (' ', '', str_replace ('-', '', trim($ccn)));
if(strlen($ccn) != 16 && !is_numeric($ccn)){
return false;
}
$checksum = 0; // running checksum total
$j = 1;
for ($i = strlen($ccn) - 1; $i >= 0; $i--) {
// Extract the next digit and multiply by 1 or 2 on alternative digits.
$calc = $ccn{$i} * $j;
// If the result is in two digits add 1 to the checksum total
if ($calc > 9) {
$checksum = $checksum + 1;
$calc = $calc - 10;
}
// Add the units element to the checksum total
$checksum = $checksum + $calc;
// Switch the value of j
if ($j ==1) {$j = 2;} else {$j = 1;};
}
if ($checksum % 10 != 0) {
return false;
}
return $ccn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment