信用卡效驗程式

來源:互聯網
上載者:User
關鍵字 信用卡效驗程式

//////////////////////////////////////////////////// 
//                        // 
// 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      $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); 
} 

?> 
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.