Location: PHPKode > scripts > Shopping basket with MySQL connection > shopping-basket-with-mysql-connection/credit_validation.php
<?
class credit_card 
{ 
    function clean_no ($cc_no) 
    { return ereg_replace ('[^0-9]+', '', $cc_no); } 

    function identify ($cc_no) 
    {  $cc_no = credit_card::clean_no ($cc_no); 
 if (ereg ('^4(.{12}|.{15})$', $cc_no)) 
            return 'Visa'; 
        if (ereg ('^5[1-5].{14}$', $cc_no)) 
            return 'Mastercard'; 
        if (ereg ('^3[47].{13}$', $cc_no)) 
            return 'American Express'; 
        if (ereg ('^3(0[0-5].{11}|[68].{12})$', $cc_no)) 
            return 'Diners Club/Carte Blanche'; 
        if (ereg ('^6011.{12}$', $cc_no)) 
            return 'Discover Card'; 
        if (ereg ('^(3.{15}|(2131|1800).{11})$', $cc_no)) 
            return 'JCB'; 
        if (ereg ('^2(014|149).{11})$', $cc_no)) 
            return 'enRoute'; 
 return 'unknown'; 
    } 
    function validate ($cc_no) 
    { 
                $cc_no = strrev (credit_card::clean_no ($cc_no)); 
         
    
        for ($ndx = 0; $ndx < strlen ($cc_no); ++$ndx) 
            $digits .= ($ndx % 2) ? $cc_no[$ndx] * 2 : $cc_no[$ndx]; 
         
      
        for ($ndx = 0; $ndx < strlen ($digits); ++$ndx) 
            $sum += $digits[$ndx]; 

     
        return ($sum % 10) ? FALSE : TRUE; 
    } 

    function check ($cc_no) 
    { 
        $valid = credit_card::validate ($cc_no); 
        $type  = credit_card::identify ($cc_no); 
        return array ($valid, $type, 'valid' => $valid, 'type' => $type); 
    } 
} 
?>
Return current item: Shopping basket with MySQL connection