On Thu, 23 Aug 2001 00:22:28 +0200, Peter Ostry <[EMAIL PROTECTED]> wrote:

>Has anyone a PHP Script for the checksum of Creditcards?
>And a Script which can identify card types based on their first numbers?
>(VISA, AMEX, MASTER, DINERS, JBC, DISCOVERY)
>
>There where a lot of JavaScript pages on the web some years ago, but I 
>have lost my local copies and the pages are vanished...
>
>
>TIA,
>Peter

Here is what I use for Visa, MasterCard, AMEX, and Discover. Feel free
to improve it by adding other card types:


<SCRIPT LANGUAGE="php">
function test_mod10 ($mycard) {

    $mylen = strlen($mycard);
    $sumdigits = 0;

    // Add doubled digits from odd/even positions in even/odd length
    for ($tx = ($mylen % 2); $tx < $mylen; $tx += 2) {
        $digit = substr($mycard, $tx, 1) * 2;
        if ($digit < 10) {
            $sumdigits += $digit;
        } else {
            $sumdigits += $digit - 9;
        }
    }

    // Add single digits from even/odd postions in even/odd length
    for ($tx = 1 - ($mylen % 2); $tx < $mylen; $tx += 2) {
        $digit = substr($mycard, $tx, 1);
        $sumdigits += $digit;
    }

    // If divisible by 10, return true
    return (($sumdigits % 10) == 0);
}


$cn = $HTTP_GET_VARS['cn'];

$tn = substr(ereg_replace("[^0-9]", "", $cn) , 0, 25);

$vl = strlen($tn);
$v1 = substr($tn, 0, 1);
$v2 = substr($tn, 0, 2);
$v4 = substr($tn, 0, 4);

$ep_cn = '';

if ($ct == 'V') {
    if (($vl != 13 && $vl != 16) || $v1 != 4 || !(test_mod10($tn))) {
        $ep_cn = "$cn is not a Visa card.";
    }

} elseif ($ct == 'M') {
    if ($vl != 16 || $v1 != 5 || !(test_mod10($tn))) {
        $ep_cn = "$cn is not a MasterCard.";
    }

} elseif ($ct == 'A') {
    if ($vl != 15 || $v2 != 37 || !(test_mod10($tn))) {
        $ep_cn = "$cn is not an AMEX card.";
    }

} elseif ($ct == 'D') {
    if ($vl != 16 || !($v4 == 6100) || !(test_mod10($tn))) {
        $ep_cn = "$cn is not a Discover card.";
    }
}
</SCRIPT>


-- 
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]

Reply via email to