Jump to content

luhn credit dard check how to capture result


geroido

Recommended Posts

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

?>

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

?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.