使用Luhn的信用卡验证

Mic*_*eal -1 php credit-card luhn

我正在使用Luhn的算法进行信用卡验证:我只需要查看该号码是否是有效的信用卡号码.

$number=clean($_GET['number']);
if (LuhnCheck($number) == 0)
{
   echo "credit card valid";
}
else
{
   echo "invalid";
}

 function LuhnCheck($strDigits)
    {
        $sum = 0;
        $alt = false;
        for($i = strlen($strDigits) - 1; $i >= 0; $i--) 
        {
            if($alt)
            {
               $temp = $strDigits[$i];
               $temp *= 2;
               $strDigits[$i] = ($temp > 9) ? $temp = $temp - 9 : $temp;
            }
            $sum += $strDigits[$i];
            $alt = !$alt;
        }
        return $sum % 10 == 0;
    }
Run Code Online (Sandbox Code Playgroud)

以上是AMEX,发现,签证和万事达卡的工作吗?我无法证实.

Can*_*ice 5

根据维基百科,Luhn算法用于所有ISO/IED-7812卡号.美国运通属于旅行和娱乐类别,因此它的数量始于3.发现,维萨和万事达卡属于银行和金融类别,因此他们的数字始于45(而且发现也是商品推销,所以他们的数字也是从数字开始6).

简而言之,如果您的代码实现了Luhn算法,是的,它将适用于American Express,Discover,Visa和Mastercard.