Jump to content

Recommended Posts

i have taken this luhn algorithm off a website.

Its a function to check credit cards, but it just doesnt seem to do the job properly

<?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;

}

?>

 

threw help from some people at php freaks they told me to change this

  $number=preg_replace('/\D/', '', $number);

 

to

 

  $number=preg_replace('/[^\D]*/', '', $number);

 

so if im trying to test this input box

 echo '<table><tr><td>Number:</td><td><input type="text" name="number"></td></tr></table>';

 

Because the way im doing just doesnt seem to work.

	  if (is_numeric(luhn_check($number)))	

 

Link to comment
https://forums.phpfreaks.com/topic/176545-solved-how-to-use-this-function/
Share on other sites

I'm not looking at the algorithm, just how you call it.

 

$cc = '3321 5678 9012 1334';
if (luhn_check($cc)) {
echo 'luhn check for '.$cc.' is True';
} else {
echo 'luhn check for '.$cc.' is False';
}
echo '<hr />';

$cc = '3321 5678 9012 1332';
if (luhn_check($cc)) {
echo 'luhn check for '.$cc.' is True';
} else {
echo 'luhn check for '.$cc.' is False';
}

 

gives:

luhn check for 3321 5678 9012 1334 is False
luhn check for 3321 5678 9012 1332 is True

I've checked the math as it works through the luhn_check() function, and this tallies.

 

Trying it with my own credit card numbers returns true for them all.

 

I don't know what you're expecting to see, but this seems to be working perfectly correctly; so what do you believe is the problem?

Something in my code, because im trying to validate it from a text input field.

Where $cc is my text field name

 $cc = isset($_POST['cc']) ? trim($_POST['cc']) : '';

Althought it always seems to say invalid for me.

 

Did you use the original version or the changed regex one.

If you got the number you are using for testing ('3321 5678 9012 1334') from this web site, what makes you think it's actually a valid number?

 

I tested with three numbers that I know to be valid... all return True. A number plucked from a web page (which doesn't indicate whether the number is valid or not) returns false.

I used the code exactly as retrieved from this link from wikipedia, which matches the code you posted in your original posting

 

The modified regexp doesn't work, it strips all the digits from the value

i was reffered to that link, but i have been testing others, and even when i test the number that was valid for your algoirthm it didnt work.

Did you use the changed version or the original algoirthm.

 

both code give me invalid.

 

  if (luhn_check($cc)) $errorCC = true;

 

im trying that one now

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.