TheJoey Posted October 5, 2009 Share Posted October 5, 2009 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))) Quote Link to comment https://forums.phpfreaks.com/topic/176545-solved-how-to-use-this-function/ Share on other sites More sharing options...
Mark Baker Posted October 5, 2009 Share Posted October 5, 2009 if (is_numeric(luhn_check($cc))) To start, the function returns a boolean true or false, not a numeric. Quote Link to comment https://forums.phpfreaks.com/topic/176545-solved-how-to-use-this-function/#findComment-930629 Share on other sites More sharing options...
TheJoey Posted October 5, 2009 Author Share Posted October 5, 2009 i forgot to add the rest sorry if (is_numeric(luhn_check($cc))){$postErrorsCredit = true; } } Quote Link to comment https://forums.phpfreaks.com/topic/176545-solved-how-to-use-this-function/#findComment-930633 Share on other sites More sharing options...
Mark Baker Posted October 5, 2009 Share Posted October 5, 2009 I repeat: To start, the function returns a boolean true or false, not a numeric.so stop testing to see if the result of the function call is numeric $postErrorsCredit = luhn_check($cc); Quote Link to comment https://forums.phpfreaks.com/topic/176545-solved-how-to-use-this-function/#findComment-930639 Share on other sites More sharing options...
TheJoey Posted October 5, 2009 Author Share Posted October 5, 2009 Well would this work in that case if (luhn_check($cc))){$postErrorsCredit = true; } } and when i test your code snippet it doesnt seem to work im testing it with this credit card number off the internet 3321 5678 9012 1334 Quote Link to comment https://forums.phpfreaks.com/topic/176545-solved-how-to-use-this-function/#findComment-930647 Share on other sites More sharing options...
Mark Baker Posted October 5, 2009 Share Posted October 5, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/176545-solved-how-to-use-this-function/#findComment-930708 Share on other sites More sharing options...
TheJoey Posted October 5, 2009 Author Share Posted October 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176545-solved-how-to-use-this-function/#findComment-930713 Share on other sites More sharing options...
Mark Baker Posted October 5, 2009 Share Posted October 5, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/176545-solved-how-to-use-this-function/#findComment-930714 Share on other sites More sharing options...
TheJoey Posted October 5, 2009 Author Share Posted October 5, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/176545-solved-how-to-use-this-function/#findComment-930719 Share on other sites More sharing options...
TheJoey Posted October 5, 2009 Author Share Posted October 5, 2009 This could be my problem. Let me try it out. Quote Link to comment https://forums.phpfreaks.com/topic/176545-solved-how-to-use-this-function/#findComment-930721 Share on other sites More sharing options...
TheJoey Posted October 5, 2009 Author Share Posted October 5, 2009 Well mine seems to be validating now, but the wrong way 3321 5678 9012 1334 which gives me no error 3321 5678 9012 1332 which gives me a error Thanks. It seems i was trying it off the wrong algorithm this whole time was making me go crazy Quote Link to comment https://forums.phpfreaks.com/topic/176545-solved-how-to-use-this-function/#findComment-930723 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.