Jump to content

Recommended Posts

hi im trying to use credit card validation Luhn algorithm.

im storing it in a seperate php and including it on my needed page

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

}

?>

 

im trying to use it like this but its not working.

if ($errorluhn) $errorluhn = "luhn validation";
$luhn = isset($_POST['luhn']) ? trim($_POST['luhn']) : '';
if (luhn_check($luhn) $errorluhn = true;

 

 

if (luhn_check($luhn) $errorluhn = true;

that line is the acutally line where im getting trouble with

Link to comment
https://forums.phpfreaks.com/topic/176183-luhn-algorithm-troubles/
Share on other sites

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.