|
perkiset
|
 |
« on: September 15, 2008, 04:41:29 PM » |
|
I post the Javascript version for this a long, long time ago - but here's a PHP function that will return either a string (VI, MC, AX etc) if a cc number is good, or a boolean false if it is not. Enjoy! <?php function validateCreditCard($workStr) { $workStr = str_replace(array('.', '|', '-'), '', $workStr); $len = strlen($workStr); if (($len < 13) or ($len > 16)) return false;
$checkVal = 0; for ($i=0; $i<$len; $i++) { // If this is an odd position in the string then I simply add the value // at <that> position to the total... if it is an even position then I // multiply it * 2 and add it to the total - if the amount is > 10 then // I add the 2 digits together for example: 2*7 is 14, but the final // value is 5 because it's 1 + 4. Also, this is not zero indexed - I count // the first digit to be, literally the oneth, so even though it's index // is zero I consider it to be an odd position. Also, as you may have // noticed from the previous little routine this all takes place against // the input value backward. Jeebers! if (($i + 1) % 2) $checkVal += $workStr[$i]; else { // the answer is false, aka zero, so this is an even number $thisVal = $workStr[$i] * 2; if ($thisVal <= 9) $checkVal += $thisVal; if ($thisVal >= 10) $checkVal += ($thisVal - 9); } } // So: if the resulting value I just created mod 10 is not zero, it's not a valid CC... if ($checkVal % 10) { return false; } $chars1 = $workStr[0]; $chars2 = substr($workStr, 0, 2); $chars3 = substr($workStr, 0, 3); $chars4 = substr($workStr, 0, 4); if ( ($chars1 == '4') && (($len == 16) || ($len == 13)) ) { return 'VI'; } // Visa if ( ($len == 16) && ($chars2 >= '51') && ($chars2 <= '55')) { return 'MC'; } // MasterCard if ( ($len == 15) && (($chars2 == '34') || ($chars2 == '37')) ) { return 'AX'; } // AmEx if ( ($chars4 == '6011') && ($len == 16) ) { return 'DI'; } // Discover if ( ((($chars3 >= '300') && ($chars3 <= 305)) || ($chars2 == '36')) && ($len == 14) ) { return 'DC'; } // Diners Club if ( ($chars2 == '38') && ($len == 14) ) { return 'CB'; } // Carte Blanche if ( (($chars4 == '2014') || ($chars4 == '2149')) && ($len == 15) ) { return 'ER'; } // En Route if ( (($chars4 == '2131') || ($chars4 == '1800')) && ($len == 15) ) { return 'JC'; } // JCB if ( ($chars1 == '3') && ($len == 16) ) { return 'JC'; } // JCB Also return false; } ?>
|
|
|
|
« Last Edit: March 31, 2009, 02:51:42 PM by perkiset »
|
Logged
|
It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
|
|
|
|
vsloathe
|
 |
« Reply #1 on: September 15, 2008, 07:41:04 PM » |
|
Ooh. Nice man.
|
|
|
|
|
Logged
|
hai
|
|
|
|
NYDAz
|
 |
« Reply #2 on: March 31, 2009, 10:28:40 AM » |
|
and how this script should be runed ? I've tried on my localhost ... got nothing I've tried CLI ... got nothing 
|
|
|
|
|
Logged
|
I'm an educated fool, with money on my mind !
|
|
|
|
perkiset
|
 |
« Reply #3 on: March 31, 2009, 10:50:48 AM » |
|
This is just a function NY, it cannot be run all by itself. So, you'd write a little PHP page that calls it, and that's what you'd call from your browser. For example (assuming you copied the above code and saved it into a file called creditCardFunction.php) <?php
require "creditCardFunction.php"; echo validateCreditCard('4111111111111111');
?>
and the page would output, 'VI' . Note that if it is not a valid credit card, you'll only get FALSE as an answer from the function.
|
|
|
|
|
Logged
|
It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
|
|
|
|
NYDAz
|
 |
« Reply #4 on: March 31, 2009, 10:58:53 AM » |
|
what a dumb I'm  I noticed, JUST NOW, that the php begin with function ! Thanks a lot perkiset for holding my hand through the Coding Journey  I'll try now to create a page with a form where I'll input the card number and hit the process button ! On that form i'll should call the function above and i should get a valid result, right ? 
|
|
|
|
|
Logged
|
I'm an educated fool, with money on my mind !
|
|
|
|
NYDAz
|
 |
« Reply #5 on: March 31, 2009, 12:31:50 PM » |
|
So ... I've compiled 3 pages cc.php - the interface callCard.php - the page where the message will be displayed Card.php - this is perkiset's function cc.php <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Credit Card - Validator / Identifier</title> </head> <body> <h4>Validate / Identify Credit Card Type</h4> <form action="callCard.php" method="post"> Please input your card number : <input name="cardNumber" type="text"/><input type="submit" value="Validate"/> </form> </body> </html>
I know that this is not the correct form (that I should used something to let the user use only numbers) - but this polished after callCard.php <html> <body> <?php require "Card.php"; $cardNumber = $_POST['cardNumber']; echo "Your credit card number is ".$cardNumber." which mean is a "; echo validateCreditCard('[color=blue]ELIMINATED BY PERK[/color]'); ?> </body> </html>
I know the 2nd echo is wrong .... i must capture it from the form .... So how can this be done ?  Waiting for some precious advices  PS : I don't know how to color the code 
|
|
|
|
« Last Edit: March 31, 2009, 12:35:57 PM by perkiset »
|
Logged
|
I'm an educated fool, with money on my mind !
|
|
|
|
perkiset
|
 |
« Reply #6 on: March 31, 2009, 12:33:51 PM » |
|
Well, if you use a form and send it up to the server then yes, you can validate that it is a valid credit card. However, if this is a client form, you might consider validating it right there before anything is sent up to you. Look at this thread and see about doing a client side validation before posting to the server: http://www.perkiset.org/forum/javascript_code_repository_examples/validatecreditcard_validateccjs-t364.0.html
|
|
|
|
|
Logged
|
It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
|
|
|
|
perkiset
|
 |
« Reply #7 on: March 31, 2009, 12:38:26 PM » |
|
Now at your second post:
First off, please check your code over thoroughly and don't put sensitive information in them. I have no idea if that CC number was valid or not, but it should not be here.
Now then: since the function returns a FALSE if the CC is not valid, and FALSE does not echo anything, you should change your code to something like this:
if (!$ccType = validateCreditCard($theCCNum)) echo 'Invalid Card'; else echo "Card type is: $ccType";
Try that, see what you get...
|
|
|
|
|
Logged
|
It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
|
|
|
|
NYDAz
|
 |
« Reply #8 on: March 31, 2009, 12:54:13 PM » |
|
First off, please check your code over thoroughly and don't put sensitive information in them. I have no idea if that CC number was valid or not, but it should not be here.
 NEED TO PAY MOOOOOOOORE ATTENTION What could this mean ? NOTICE : Use of undefined constant len - assumed 'len'I guess it has a connection with the $len array from the function !
|
|
|
|
« Last Edit: March 31, 2009, 01:05:53 PM by NYDAz »
|
Logged
|
I'm an educated fool, with money on my mind !
|
|
|
|
NYDAz
|
 |
« Reply #9 on: March 31, 2009, 01:33:13 PM » |
|
 my first script ! BIG THANKS perkiset !  uploaded to my server and it works like a charm ! so this is my final code for callCard.php <?php require "Card.php"; $cardNumber = $_POST['cardNumber']; if (!$workStr = validateCreditCard($cardNumber)) echo "Invalid Credit Card"; else echo "Your credit card number is ".$cardNumber." ! By the way, did you know that your card type is : ".$workStr." ?"; ?>
I should buy you a  ! You were right about the cache !  PS : How can I disable the notices on my local server ? it's recommended ?
|
|
|
|
|
Logged
|
I'm an educated fool, with money on my mind !
|
|
|
|
perkiset
|
 |
« Reply #10 on: March 31, 2009, 02:54:09 PM » |
|
What could this mean ? NOTICE : Use of undefined constant len - assumed 'len'
Wow, been using that function forever, and having my error reporting set to high I never saw that. In this line of the function: for ($i=0; $i<$len; $i++) I had forgotten to put a $ in front of len - so it was letting you know that it was using the variable instead of a constant, because there is no constant defined by the name of "len." Good eye NY, I corrected the function on top so it will be right in the future. Congrats on the working page! That's a delicious moment, I know it well. Well done NYD.
|
|
|
|
|
Logged
|
It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
|
|
|
|
NYDAz
|
 |
« Reply #11 on: April 03, 2009, 09:58:45 AM » |
|
just readed about the include function 
|
|
|
|
|
Logged
|
I'm an educated fool, with money on my mind !
|
|
|
|
perkiset
|
 |
« Reply #12 on: May 04, 2009, 03:10:04 PM » |
|
Wow, holy smokes. Note above, how NYD had pointed out that I had neglected the $ in the for/next loop. This, as it turns out was really a problem. When I added the $ back in, then it started evaluating that loop as it was supposed to - and cc validation failed, because when I converted this code over from my JS I did it incorrectly. Here is the updated (and working correctly) function: <?php
function validateCreditCard($workStr) { $workStr = preg_replace('~[\.\|\-\ ]~', '', $workStr); // if the string is not 13..16 digits exactly it is not valid $len = strlen($workStr); if (($len < 13) or ($len > 16)) return false;
$checkVal = 0; $testStr = strrev($workStr); for ($i=0; $i<$len; $i++) { // If this is an odd position in the string then I simply add the value // at <that> position to the total... if it is an even position then I // multiply it * 2 and add it to the total - if the amount is > 10 then // I add the 2 digits together for example: 2*7 is 14, but the final // value is 5 because it's 1 + 4. Also, this is not zero indexed - I count // the first digit to be, literally the oneth, so even though it's index // is zero I consider it to be an odd position. Also, as you may have // noticed from the previous little routine this all takes place against // the input value backward. Jeebers! if (($i + 1) % 2) $checkVal += $testStr[$i]; else { // the answer is false, aka zero, so this is an even number $thisVal = $testStr[$i] * 2; if ($thisVal <= 9) $checkVal += $thisVal; if ($thisVal >= 10) { $checkStr = "$thisVal"; $thisVal += ($thisVal[0] + $thisVal[1]); } } } // So: if the resulting value I just created mod 10 is not zero, it's not a valid CC... if ($checkVal % 10) { return false; } $chars1 = $workStr[0]; $chars2 = substr($workStr, 0, 2); $chars3 = substr($workStr, 0, 3); $chars4 = substr($workStr, 0, 4); if ( ($chars1 == '4') && (($len == 16) || ($len == 13)) ) { return 'VI'; } // Visa if ( ($len == 16) && ($chars2 >= '51') && ($chars2 <= '55')) { return 'MC'; } // MasterCard if ( ($len == 15) && (($chars2 == '34') || ($chars2 == '37')) ) { return 'AX'; } // AmEx if ( ($chars4 == '6011') && ($len == 16) ) { return 'DI'; } // Discover if ( ((($chars3 >= '300') && ($chars3 <= 305)) || ($chars2 == '36')) && ($len == 14) ) { return 'DC'; } // Diners Club if ( ($chars2 == '38') && ($len == 14) ) { return 'CB'; } // Carte Blanche if ( (($chars4 == '2014') || ($chars4 == '2149')) && ($len == 15) ) { return 'ER'; } // En Route if ( (($chars4 == '2131') || ($chars4 == '1800')) && ($len == 15) ) { return 'JC'; } // JCB if ( ($chars1 == '3') && ($len == 16) ) { return 'JC'; } // JCB Also return false; }
?>

|
|
|
|
|
Logged
|
It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
|
|
|
|
perkiset
|
 |
« Reply #13 on: May 10, 2009, 05:36:29 PM » |
|
FFS I've had trouble with this function. I just can't believe how wrong I had it, here it is after I've run a couple hundred CCs through it to verify that I've got it right. <?php
function validateCreditCard($workStr) { $workStr = preg_replace('~[\.\|\-\ ]~', '', $workStr); // if the string is not 13..16 digits exactly it is not valid $len = strlen($workStr); if (($len < 13) or ($len > 16)) return false;
$checkVal = 0; $testStr = strrev($workStr); for ($i=0; $i<$len; $i++) { // If this is an odd position in the string then I simply add the value // at <that> position to the total... if it is an even position then I // multiply it * 2 and add it to the total - if the amount is > 10 then // I add the 2 digits together for example: 2*7 is 14, but the final // value is 5 because it's 1 + 4. Also, this is not zero indexed - I count // the first digit to be, literally the oneth, so even though it's index // is zero I consider it to be an odd position. Also, as you may have // noticed from the previous little routine this all takes place against // the input value backward. Jeebers! if (($i + 1) % 2) { $checkVal += $testStr[$i]; } else { // the answer is false, aka zero, so this is an even number $thisVal = $testStr[$i] * 2; if ($thisVal <= 9) { $checkVal += $thisVal; } else { $checkStr = "$thisVal"; $checkVal += ($checkStr[0] + $checkStr[1]); } } } // So: if the resulting value I just created mod 10 is not zero, it's not a valid CC... if ($checkVal % 10) { return false; } $chars1 = $workStr[0]; $chars2 = substr($workStr, 0, 2); $chars3 = substr($workStr, 0, 3); $chars4 = substr($workStr, 0, 4); if ( ($chars1 == '4') && (($len == 16) || ($len == 13)) ) { return 'VI'; } // Visa if ( ($len == 16) && ($chars2 >= '51') && ($chars2 <= '55')) { return 'MC'; } // MasterCard if ( ($len == 15) && (($chars2 == '34') || ($chars2 == '37')) ) { return 'AX'; } // AmEx if ( ($chars4 == '6011') && ($len == 16) ) { return 'DI'; } // Discover if ( ((($chars3 >= '300') && ($chars3 <= 305)) || ($chars2 == '36')) && ($len == 14) ) { return 'DC'; } // Diners Club if ( ($chars2 == '38') && ($len == 14) ) { return 'CB'; } // Carte Blanche if ( (($chars4 == '2014') || ($chars4 == '2149')) && ($len == 15) ) { return 'ER'; } // En Route if ( (($chars4 == '2131') || ($chars4 == '1800')) && ($len == 15) ) { return 'JC'; } // JCB if ( ($chars1 == '3') && ($len == 16) ) { return 'JC'; } // JCB Also return false; }
?>
|
|
|
|
|
Logged
|
It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
|
|
|
|
NYDAz
|
 |
« Reply #14 on: May 11, 2009, 12:31:41 PM » |
|
I'm wondering why you reviewed this script ? PS : The new version look much smoother 
|
|
|
|
|
Logged
|
I'm an educated fool, with money on my mind !
|
|
|
|