欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

信用卡效验程序

程序员文章站 2022-06-15 08:13:55
<?php  

////////////////////////////////////////////////////  
//                                                //   
// credit card validation routine                 //  
// may 15, 2000                                   //  
// by ariso                                       //  
// validatecardcode($number[,$cardtype])          //  
////////////////////////////////////////////////////  


function validatecardcode($cardnumber, $cardtype =  'unknown')
{  
     //clean up input  

    $cardtype = strtolower($cardtype);  
    $cardnumber = ereg_replace( '[-[:space:]]',  '',$cardnumber);   

     //do type specific checks  

    if ($cardtype ==  'unknown') {  
         //skip type specific checks  
    }  
    elseif ($cardtype ==  'mastercard'){  
        if (strlen($cardnumber) != 16 || !ereg( '5[1-5]', $cardnumber)) return 0;  
    }  
    elseif ($cardtype ==  'visa'){  
        if ((strlen($cardnumber) != 13 && strlen($cardnumber) != 16) || substr($cardnumber, 0, 1) !=  '4')  

return 0;  
    }  
    elseif ($cardtype ==  'amex'){  
        if (strlen($cardnumber) != 15 || !ereg( '3[47]', $cardnumber)) return a;  
    }  
    elseif ($cardtype ==  'discover'){  
        if (strlen($cardnumber) != 16 || substr($cardnumber, 0, 4) !=  '6011') return 0;  
    }  
    else {  
         //invalid type entered  
        return -1;  
    }  


     // start mod 10 checks  

    $dig = tochararray($cardnumber);  
    $numdig = sizeof ($dig);  
    $intintj = 0;  
    for ($inti=($numdig-2); $inti>=0; $inti-=2){  
        $dbl[$intintj] = $dig[$inti] * 2;  
        $intintj++;  
    }      
    $dblsz = sizeof($dbl);  
    $validate =0;  
    for ($inti=0;$inti<$dblsz;$inti++){  
        $add = tochararray($dbl[$inti]);  
        for ($intintj=0;$intintj<sizeof($add);$intintj++){  
            $validate += $add[$intintj];  
        }  
    $add =  '';  
    }  
    for ($inti=($numdig-1); $inti>=0; $inti-=2){  
        $validate += $dig[$inti];   
    }  
    if (substr($validate, -1, 1) ==  '0') return 1;  
    else return 0;  
}  


// takes a string and returns an array of characters  

function tochararray($intinput){  
    $len = strlen($intinput);  
    for ($intintj=0;$intintj<$len;$intintj++){  
        $char[$intintj] = substr($intinput, $intintj, 1);      
    }  
    return ($char);  
}  

?>