Re: Credit card number checker?
| From: | Nathan Cook | Date: | Mon, 16 Jul 2001 22:48:29 +0000 |
| Subject: | Re: Credit card number checker? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-58130@lists.php.net to get a copy of this message | ||
I ran across this function awhile back and it is what we use.
-------------------------------------------------------------------
function CCValidationSolution ($Number) {
global $CardName;
# 1) Get rid of spaces and non-numeric characters.
$Number = OnlyNumericSolution($Number);
# 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 >= 4000 and $NumberLeft <= 4999) {
$CardName = "Visa";
if ($NumberLength > 14) {
$ShouldLength = 16;
} elseif ($NumberLength < 14) {
$ShouldLength = 13;
} else {
return FALSE;
}
} elseif ($NumberLeft >= 5100 and $NumberLeft <= 5599) {
$CardName = "MasterCard";
$ShouldLength = 16;
} else {
return FALSE;
}
# 3) Is the number the right length?
if ($NumberLength <> $ShouldLength) {
$Missing = $NumberLength - $ShouldLength;
if ($Missing < 0) {
} else {
}
return FALSE;
}
# 4) Does the number pass the Mod 10 Algorithm Checksum?
if (Mod10Solution($Number) == TRUE) {
return TRUE;
} else {
return FALSE;
}
}
function OnlyNumericSolution ($Number) {
# Remove any non numeric characters.
# Ensure number is no more than 19 characters long.
return substr( ereg_replace( "[^0-9]", "", $Number) , 0, 19);
}
function Mod10Solution ($Number) {
$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);
}
// Usage
if (CCValidationSolution($Number) == TRUE) {
echo "GOOD";
} else {
echo "Bad card!";
}
-------------------------------------------------------------------
Good Luck!
Nathan Cook
ncook@pcsedu.com
----- Original Message -----
From: "Dr. Evil" <drevil@sidereal.kz>
To: <php-general@lists.php.net>
Sent: Monday, July 16, 2001 4:12 PM
Subject: [PHP] Credit card number checker?
>
> I know that credit cards have standard formats: There's a standard
> number of digits, and whether the card is Visa, MC, Amex, etc is
> encoded in the number, and there is some kind of checksum, and I think
> the expiration is also encoded in the number. All of this is obvious
> stuff that anyone designing such a system would do.
>
> I'm wondering if anyone can refer me to a site that describes what
> this format is, so I can write some PHP code that will check to see if
> a credit card number format is correct. I don't even want to try to
> run the card through my merchant account if the format is obviously
> wrong. I assume that banks check the rejection rate on their merchant
> accounts, and too many bogus cards would not look good.
>
> I did a quick search on the web, and there are a vast number of
> "hacker" credit card number generators, but that isn't exactly what
> I'm looking for.
>
> Thanks
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
>