Here's what I pulled from px.sklar.com and modified to suit my own
devious ends... :)
<?
Function verifyCreditCard ($Number, $creditcardtype = "")
{
// Nicked this from:
http://www.analysisandsolutions.com/code/ccvs-ph.htm
// Checks the 1st 4 digits for validity, then checks
// that the card number is the right length, and checks
// the mod10 test, and finally checks that the card type
// is the card type the user input.
// Returns an array:
// - [success] 0 or 1 for a valid card
// - [reason] text explaining why the card's not valid
// - [callifvalid] redundant and not used.
// Step 1 - Get rid of spaces and non-numeric characters.
$Number = ereg_replace("[^0-9]", "", $Number);
// Step 2 - Do the first four digits fit within proper ranges?
// If so, who's the card issuer and how long should the number
be?
$NumberLeft = substr($Number, 0, 4);
$NumberLength = strlen($Number);
if ($NumberLeft >= 3000 and $NumberLeft <= 3059)
{
$CardName = "Diners Club";
$checkcard = "DINERS";
$ShouldLength = 14;
}
elseif ($NumberLeft >= 3600 and $NumberLeft <= 3699)
{
$CardName = "Diners Club";
$checkcard = "DINERS";
$ShouldLength = 14;
}
elseif ($NumberLeft >= 3800 and $NumberLeft <= 3889)
{
$CardName = "Diners Club";
$checkcard = "DINERS";
$ShouldLength = 14;
}
elseif ($NumberLeft >= 3400 and $NumberLeft <= 3499)
{
$CardName = "American Express";
$checkcard = "AMEX";
$ShouldLength = 15;
}
elseif ($NumberLeft >= 3700 and $NumberLeft <= 3799)
{
$CardName = "American Express";
$checkcard = "AMEX";
$ShouldLength = 15;
}
//elseif ($NumberLeft >= 3528 and $NumberLeft <= 3589)
//{
// $CardName = "JCB";
// $ShouldLength = 16;
//}
//elseif ($NumberLeft >= 3890 and $NumberLeft <= 3899)
//{
// $CardName = "Carte Blache";
// $ShouldLength = 14;
//}
elseif ($NumberLeft >= 4000 and $NumberLeft <= 4999)
{
$CardName = "Visa";
$checkcard = "VISA";
if ($NumberLength > 14)
{
$ShouldLength = 16;
}
elseif ($NumberLength < 14)
{
$ShouldLength = 13;
}
else
{
$debug .= "<br /><em>The Visa number entered, $Number, in is 14 \n"
."digits long.<br />Visa cards usually have 16 digits, \n"
."though some have 13.<br />Please check the number and
\n"
."try again.</em><br />\n";
$result = 0;
$reason = "Visa cards have either 16 or 13 digits";
}
}
elseif ($NumberLeft >= 5100 and $NumberLeft <= 5599)
{
$CardName = "MasterCard";
$checkcard = "MASTERCARD";
$ShouldLength = 16;
}
elseif ($NumberLeft == 5610)
{
$CardName = "Australian BankCard";
$checkcard = "BANKCARD";
$ShouldLength = 16;
}
//elseif ($NumberLeft == 6011)
//{
// $CardName = "Discover/Novus";
// $ShouldLength = 16;
//}
else
{
$debug .= "<br /><em>The first four digits of the number entered are
\n"
."$NumberLeft. <br />If that's correct, we don't accept that
\n"
."type of credit card.<br />If it's wrong, please try
again.\n"
."</em><br />\n";
//return FALSE;
$result = 0;
$reason = "Your card number was not recognised as valid";
$callifvalid = 1;
}
// Step 3 - Is the number the right length?
if ($NumberLength <> $ShouldLength)
{
$Missing = $NumberLength - $ShouldLength;
if ($Missing < 0)
{
$debug .= "<br /><em>The $CardName number entered, $Number, is
missing \n"
.abs($Missing)." digit(s).<br />Please check the number
and \n"
."try again.</em><br />\n";
}
else
{
$debug .= "<br /><em>The $CardName number entered, $Number, has \n"
."$Missing too many digit(s).<br />Please check the number
\n"
."and try again.</em><br />\n";
}
$result = 0;
$failed = 1;
$reason = "Your card number has the wrong number of digits";
}
// Step 4 - Does the number pass the Mod 10 Algorithm Checksum?
if (ccVerifyMod10($Number) == TRUE)
{
$debug .= "Passed mod10";
$result = 1;
}
else
{
$debug .= "<br /><em>The $CardName number entered, $Number, is \n"
."invalid.<br />Please check the number and try
again.</em><br />\n";
$failed = 1;
$result = 0;
$reason = "Your card number is invalid";
}
if ($failed)
{ $result = 0; }
// Step 5 - Does the card type match the user's selected card type?
if ($creditcardtype)
{
if ($creditcardtype != $checkcard)
{
$result = 0;
$reason = "Your card number and card type don't match";
}
}
$return[result] = $result;
$return[reason] = $reason;
$return[callifvalid] = $callifvalid;
//echo $debug;
return $return;
}
Function ccVerifyMod10 ($Number)
{
// NOTE: This function relies heavily on the use of Magic(tm).
// I have no idea how it works, I just know that it does.
$NumberLength = strlen($Number);
$Checksum = 0;
// Add even digits in even length strings,
// or odd digits in odd length strings.
for ($Location = 1 - ($NumberLength % 2); $Location < $NumberLength;
$Location += 2)
{
$Checksum += substr($Number, $Location, 1);
}
# Analyze odd digits in even length strings
# or even digits in odd length strings.
for ($Location = ($NumberLength % 2); $Location < $NumberLength;
$Location += 2)
{
$Digit = substr($Number, $Location, 1) * 2;
if ($Digit < 10)
{
$Checksum += $Digit;
}
else
{
$Checksum += $Digit - 9;
}
}
# Is the checksum divisible by ten?
return ($Checksum % 10 == 0);
}
?>
Enjoy...
--
Jason Murray
[EMAIL PROTECTED]
Web Developer, Melbourne IT
"Work now, freak later!"
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]