Sindbad~EG File Manager

Current Path : /home/beeson/public_html/helpwithmath_old/
Upload File :
Current File : //home/beeson/public_html/helpwithmath_old/ValidateCreditCard.php

<?php 
function validateCreditCard($cc_type,$value)
// $ptype should be "Visa" or "MasterCard" or "American Express" 
// return "true" or an error message after checking number of digits, prefix, and Luhn checksum
{ // First, get rid of any spaces or dashes the user might have typed in,  then test the result to see if it's a numeric value.
  $isValid = true;
  $cc_number = ereg_replace('[^[:digit:]]','',$value);  // strip all non-numbers out
  switch($cc_type)
  { case "MasterCard":
       $isValid = ereg('^5[1-5].{14}$',$cc_number);
       break;
    case "Visa":
       $isValid = ereg('^4/{15}$|^4.{12}$',$cc_number);
       break;
    case "American Express":
       $isValid = ereg('^3[47].{13}$',$cc_number);
       break;
	case "Discover":
	   $isValid = ereg('^6011.{12}$',$cc_number);
	   break;	   
	default: 
	   return "Unknown credit card type " . $cc_type; 
  }
  if(!isValid)
     return "That is not a valid $cc_type number. Either the number or the credit card type is wrong.";
      // check to the if the number has the correct prefix for that type:
  // Now perform the checksum test using the Luhn formula. 
  // First we make a copy of the number, reversing the order of the digits.
  $copy = strrev($cc_number);
  $length = strlen($cc_number);
  $sum = 0;
  for($c = 0; $c < $length; $c++)
     { // As we add up the digits, we double every other digit starting with the second one from the left, 
       // since we've reversed their order. Since the first digit the first digit has the index 0, this means 
       // we'll be doubling the odd-numbered ones. The first digit can be obtained as substr($digit,0,1), 
       // the second one as substr($digit,1,1), and so on through substr($digit,$length-1,1):
      $digit = substr($copy,$c,1);
      if($c % 2 == 1)
         { $digit *= 2;  // double if odd
           if($digit > 9)
              { $first = substr($digit,0,1); 
			    $second = substr($digit,1,1);
                $digit = $first + $second;
              }
         }
      $sum += $digit;
    }
  if($sum % 10 != 0)
     return "That is not a valid credit card number. <br>";
  return "true";  
}

?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists