geroido Posted August 23, 2008 Share Posted August 23, 2008 Hi I'm currently at the stage of doing a simple credit card check. As I say, I'm relatively new to PHP and have downloaded this php script. I will study and understand it however, I have a couple of questions. Can I assume that $credit_card is the variable that holds my credit card number? Also can you tell me how do I access the result of the check. The function returns true or false but what code do I need to determine the result? Thanks <?php function ValidCard($credit_card) { $checksum = substr ($credit_card, -1); // returns checksum $restnum = substr($credit_card, 0, strlen($credit_card)-1); // returns rest of credit card number $calcvals = ""; for ($i = strlen($restnum)+1; $i >= 0; $i=$i-2) { $calcvals = $calcvals . strval(intval(substr($restnum,$i,1))*2); } for ($i = strlen($restnum) -2; $i >= 0; $i=$i-2) { $calcvals = $calcvals . strval(intval(substr($restnum,$i,1))*1); } $calcvals = substr($calcvals,1); $count = 0; for ($i = 0; $i <= strlen($calcvals); $i++) { $count = $count + intval(substr($calcvals,$i,1)); } if(intval(substr(strval((10*(intval(substr(strval ($count),0,1))+1)-$count)),-1))==$checksum) { return true; } else { return false; } } ?> Link to comment https://forums.phpfreaks.com/topic/120983-luhn-credit-dard-check-how-to-capture-result/ Share on other sites More sharing options...
Fadion Posted August 23, 2008 Share Posted August 23, 2008 I should be: <?php $card = 'xxxx-xxxx-xxxx'; if(ValidCard($card)){ //check if the function returns true echo 'Card valid'; } else{ echo 'Card invalid'; } ?> Take a look here for some information about functions. Link to comment https://forums.phpfreaks.com/topic/120983-luhn-credit-dard-check-how-to-capture-result/#findComment-623707 Share on other sites More sharing options...
geroido Posted August 23, 2008 Author Share Posted August 23, 2008 Hi GuiltyGear I don't understand, where would I put your code. The function is already returning true or false. I just want to know if it's true or false. I need to capture the returned value <?php function ValidCard($credit_card) { $checksum = substr ($credit_card, -1); // returns checksum $restnum = substr($credit_card, 0, strlen($credit_card)-1); // returns rest of credit card number $calcvals = ""; for ($i = strlen($restnum)+1; $i >= 0; $i=$i-2) { $calcvals = $calcvals . strval(intval(substr($restnum,$i,1))*2); } for ($i = strlen($restnum) -2; $i >= 0; $i=$i-2) { $calcvals = $calcvals . strval(intval(substr($restnum,$i,1))*1); } $calcvals = substr($calcvals,1); $count = 0; for ($i = 0; $i <= strlen($calcvals); $i++) { $count = $count + intval(substr($calcvals,$i,1)); } if(intval(substr(strval((10*(intval(substr(strval ($count),0,1))+1)-$count)),-1))==$checksum) { return true; } else { return false; } } ?> Link to comment https://forums.phpfreaks.com/topic/120983-luhn-credit-dard-check-how-to-capture-result/#findComment-623710 Share on other sites More sharing options...
Fadion Posted August 23, 2008 Share Posted August 23, 2008 That's what the code i suggested will do. Are you really that new to php, that you don't know how to call a function? <?php function ValidCard($credit_card) { $checksum = substr ($credit_card, -1); // returns checksum $restnum = substr($credit_card, 0, strlen($credit_card)-1); // returns rest of credit card number $calcvals = ""; for ($i = strlen($restnum)+1; $i >= 0; $i=$i-2) { $calcvals = $calcvals . strval(intval(substr($restnum,$i,1))*2); } for ($i = strlen($restnum) -2; $i >= 0; $i=$i-2) { $calcvals = $calcvals . strval(intval(substr($restnum,$i,1))*1); } $calcvals = substr($calcvals,1); $count = 0; for ($i = 0; $i <= strlen($calcvals); $i++) { $count = $count + intval(substr($calcvals,$i,1)); } if(intval(substr(strval((10*(intval(substr(strval ($count),0,1))+1)-$count)),-1))==$checksum) { return true; } else { return false; } } //you call the function after this line $card = 'xxxx-xxxx-xxxx'; if(ValidCard($card)){ //check if the function returns true echo 'Card valid'; } else{ echo 'Card invalid'; } ?> Link to comment https://forums.phpfreaks.com/topic/120983-luhn-credit-dard-check-how-to-capture-result/#findComment-623720 Share on other sites More sharing options...
PFMaBiSmAd Posted August 23, 2008 Share Posted August 23, 2008 I don't know if this will help: http://www.w3schools.com/php/php_functions.asp Link to comment https://forums.phpfreaks.com/topic/120983-luhn-credit-dard-check-how-to-capture-result/#findComment-623723 Share on other sites More sharing options...
geroido Posted August 23, 2008 Author Share Posted August 23, 2008 Yes, I'm afraid I am. Also got confused that you were using a variable called $card when mine was called $credit_card. That threw me for a while. got it working now. Thanks. Functions never my strong point. Link to comment https://forums.phpfreaks.com/topic/120983-luhn-credit-dard-check-how-to-capture-result/#findComment-623728 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.