Re: checking of credit card num.
| From: | Colin Putney | Date: | Mon, 27 Mar 2000 02:34:20 +0000 |
| Subject: | Re: checking of credit card num. | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-17267@lists.php.net to get a copy of this message | ||
-- John <john@edlabs.com> wrote:
> hi,
> is there any way i can validate the credit card num?
Hi John,
The function below returns the type of the credit card, based on the
number. If it's an invalid number it returns false. This version only
does Visa, Mastercard and American Express, but it's easy to add in any
other cards that you accept.
HTH,
Colin
function GetCardType($ccnum) {
// initialize variables
$sum = 0;
$mul = 1;
$l = strlen($ccnum);
// calculate checksum
for ( $i=0; $i<$l; $i++) {
$digit = $ccnum[$l-$i-1];
$tproduct = $digit * $mul;
if ($tproduct >= 10)
$sum += ($tproduct % 10) + 1;
else
$sum += $tproduct;
if ($mul == 1)
$mul++;
else
$mul--;
}
// screen checksum
if (($sum % 10) != 0)
return false;
// check for Visa
if ( (($l==16) || ($l==13)) && ($ccnum[0]==4) )
return "VI";
// check for Master Card
if ( ($l==16) && ($ccnum[1]==5) && ($ccnum[1]>=1) && ($ccnum[1]<=5) )
return "CA";
// check for Amex
if ( ($l==15) && ($ccnum[0]==3) && (($ccnum[1]==4) || ($ccnum[1]==7)) )
return "AX";
// if we got this far, then no card matched
return false;
}
---------------------------------------------
Colin Putney cputney@whistler.net
Whistler Networks http://www.whistler.net/