Mega Code Archive

 
Categories / Php / Authentication
 

Validation function for LUHNMod10 and variant. Can discriminate credit card

numbers of varying lengths. <? function prestoLUHN($ccn="",$alt="0") { // pre-validations $ccn = ereg_replace("[^[:digit:]]+", "", $ccn); if ((strlen($ccn) < 1)) return FALSE; // Mapping: Double-then-SumOfDigits (0123456789) => (0246813579) for($i=0;$i<=9;$i++) $v[$i] = (($i*2)<9) ? ($i*2):($i*2)-9; // Initial value of $alt (Alternation Pattern) determines which digits are transformed by $v[] during checksum calculation // 0 = apply transform to even-numbered digits from LSD-to-MSD (Conventional LUHNMod10) // 1 = apply transform to odd-numbered digits from LSD-to-MSD (alternative) $checksum = 0; // Calculate checksum from the end of the string (the low-order digit) to beginning of string for($i = strlen($ccn)-1; $i >= 0; $i--) { $digit = substr($ccn, $i, 1); $checksum += $alt ? $v[$digit] : $digit; $alt ^= 1; } // return the checksum descrimination result return !($checksum % 10); }; ?>