php3-list | 199807

Re: [PHP3] Credit Card From: Stefan Powell (spowell <email protected>)
Date: 07/30/98

Guoneng Zhong wrote:
>
> Hi,
> Would anyone know the credit card verification algorithm or where it can
> be found? I am writing part of a shopping cart and I want to make sure
> the credit card number is at least valid. Thanks!

You'll need to check the length, the start digits and a LUHN Mod 10
checksum.
Here is a good document that outlines the checks for various cards...
http://www.beachnet.com/~hstiles/cardtype.html

AND (drum roll please...)
------------SNIP!----------------
<?php
//Stefan's slightly obfuscated, but much improved LUHNMod10
function LUHNMod10($ccnum) {
  if (!isset($ccnum)) return FALSE;
  $sum = 0;
  $len = strlen($ccnum);
  $ccnum=ereg_replace('[^[:digit:]]+','',$ccnum);
  for($idx=0;$idx<$len;$idx++) {
    $digit = substr($ccnum,$idx,1);
    if ($idx%2) {$sum += ($digit >= 5) ? 1+(($digit*2)%10) : $digit*2;}
    else {$sum += $digit;}
  }
  if ($sum%10) return FALSE;
  return TRUE;
}

if (LUHNMod10($ccn)) {
  echo "OK! Lets go shoppin!";
}
else {
  echo "WhuchutalkinabouWillis?";
}
  
?>

<form action="<?php echo $SCRIPT_NAME ?>">
<input type="text" name="ccn">
</form>

------------SNIP!----------------

There you go.

BTW, if anyone is having trouble with that algorithm just send me your
credit card number and I'll test it for you. ;-)

--Stef

--
PHP 3 Mailing List   http://www.php.net/
To unsubscribe send an empty message to php3-unsubscribe <email protected>
To subscribe to the digest list:  php3-digest-subscribe <email protected>
For help: php3-help <email protected>  Archive:  http://www.php.net/mailsearch.php3