PHP Luhn modulus implementation

The following luhn_check() function is a small PHP function which checks whether a number is a valid based on the Luhn algorithm. One common example of use is with credit and debit card numbers. This function may be used to check for valid credit card numbers, however additional checks may be desired, such as card prefix and length checks.



<?php
/* Luhn algorithm number checker - (c) 2005-2008 - planzero.org            *
 * This code has been released into the public domain, however please      *
 * give credit to the original author where possible.                      */
 
function luhn_check($number) {
 
  // Strip any non-digits (useful for credit card numbers with spaces and hyphens)
  $number=preg_replace('/\D/', '', $number);
 
  // Set the string length and parity
  $number_length=strlen($number);
  $parity=$number_length % 2;
 
  // Loop through each digit and do the maths
  $total=0;
  for ($i=0; $i<$number_length; $i++) {
    $digit=$number[$i];
    // Multiply alternate digits by two
    if ($i % 2 == $parity) {
      $digit*=2;
      // If the sum is two digits, add them together (in effect)
      if ($digit > 9) {
        $digit-=9;
      }
    }
    // Total up the digits
    $total+=$digit;
  }
 
  // If the total mod 10 equals 0, the number is valid
  return ($total % 10 == 0) ? TRUE : FALSE;
 
}
 
?>
Share and enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Slashdot
  • StumbleUpon
  • Twitter
  • MySpace
This entry was written by Plan Zero , posted on Wednesday August 26 2009at 10:08 pm , filed under Code Snippets . Bookmark the permalink . Post a comment below or leave a trackback: Trackback URL.

6 Responses to “PHP Luhn modulus implementation”

  1. Thanks for the reference, here is a function to generate a Luhn checksum:

    function Luhn($number, $iterations = 1)
    {
    while ($iterations– >= 1)
    {
    $stack = 0;
    $number = str_split(strrev($number), 1);

    foreach ($number as $key => $value)
    {
    if ($key % 2 == 0)
    {
    $value = array_sum(str_split($value * 2, 1));
    }

    $stack += $value;
    }

    $stack %= 10;

    if ($stack != 0)
    {
    $stack -= 10;
    }

    $number = implode(”, array_reverse($number)) . abs($stack);
    }

    return $number;
    }

    A validation that uses this function can also be easily coded.

  2. thanks for the algorithm….

  3. Nice one…..

  4. function luhn_validate($input, $mod5 = false){
    $total = 0;
    settype($input,’string’);
    $length = strlen((string)$input);
    $parity = $length % 2;
    for($i = 0;$i 9 ? $x – 9 : $x) : (int)$input[$i]);
    return (($total % ((boolean)$mod5 ? 5 : 10)) == 0);
    }

  5. Wow my bad, I just copied and pasted my code and didn’t pay attention to the greater than and less than signs. It should actually look like this:
    function luhn_validate($input, $mod5 = false){
    $total = 0;
    settype($input,’string’);
    $length = strlen((string)$input);
    $parity = $length % 2;
    for($i = 0;$i < $length;$i++) $total += ($i % 2 === $parity ? (($x = (int)$input[$i] * 2) > 9 ? $x – 9 : $x) : (int)$input[$i]);
    return (($total % ((boolean)$mod5 ? 5 : 10)) == 0);
    }

  6. >>>>>>>>>>>> http://www.duam.com.mx <<<<<<<<<<<<<<<

Leave a Reply