2011-12-13 57 views
1

嗨,我使用codeigniter。我想验证我的信用卡信息。我看到在PHP中有类来验证信用卡号码。我看见笨一个帮手来验证信用卡什么是验证信用卡codeigniter

http://codeigniter.com/wiki/Credit_Card_Helper

/** 
* Truncates a card number retaining only the first 4 and the last 4 digits. It then returns the truncated form. 
* 
* @param string The card number to truncate. 
* @return string The truncated card number. 
*/ 
function truncate_card($card_num) { 
    $padsize = (strlen($card_num) < 7 ? 0 : strlen($card_num) - 7); 
    return substr($card_num, 0, 4) . str_repeat('X', $padsize). substr($card_num, -3); 
} 


/** 
* Validates a card expiry date. Finds the midnight on first day of the following 
* month and ensures that is greater than the current time (cards expire at the 
* end of the printed month). Assumes basic sanity checks have already been performed 
* on month/year (i.e. length, numeric, etc). 
* 
* @param integer The expiry month shown on the card. 
* @param integer The expiry year printed on the card. 
* @return boolean Returns true if the card is still valid, false if it has expired. 
*/ 
function card_expiry_valid($month, $year) { 
    $expiry_date = mktime(0, 0, 0, ($month + 1), 1, $year); 
    return ($expiry_date > time()); 
} 


/** 
* Strips all non-numerics from the card number. 
* 
* @param string The card number to clean up. 
* @return string The stripped down card number. 
*/ 
function card_number_clean($number) { 
    return ereg_replace("[^0-9]", "", $number); 
} 


/** 
* Uses the Luhn algorithm (aka Mod10) <http://en.wikipedia.org/wiki/Luhn_algorithm> 
* to perform basic validation of a credit card number. 
* 
* @param string The card number to validate. 
* @return boolean True if valid according to the Luhn algorith, false otherwise. 
*/ 
function card_number_valid ($card_number) { 
    $card_number = strrev(card_number_clean($card_number)); 
    $sum = 0; 

    for ($i = 0; $i < strlen($card_number); $i++) { 
     $digit = substr($card_number, $i, 1); 

     // Double every second digit 
     if ($i % 2 == 1) { 
      $digit *= 2; 
     } 

     // Add digits of 2-digit numbers together 
     if ($digit > 9) { 
      $digit = ($digit % 10) + floor($digit/10); 
     } 

     $sum += $digit; 
    } 

    // If the total has no remainder it's OK 
    return ($sum % 10 == 0); 
} 
?> 

它采用了常见的验证。但我想根据卡类型这样

http://www.braemoor.co.uk/software/creditcard.php

验证是否有任何的CodeIgniter库或助手。请帮助.....................

+0

可能重复(HTTP [什么是验证在PHP中的信用卡最好的方法是什么?] //stackoverflow.com/questions/174730/what-is-the-best-way-to-validate-a-credit-card-in-php)适用于PHP – ajreal

+0

您是否想过将您链接的代码移植到CI插件? –

+0

@Jared Farrish,还没有,我想知道codeigniter中是否有现有的。如果没有,我会将它移植到codeigniter –

回答

7

正如人们已经告诉过你的,CodeIgniter是一个php框架,使用php编码,在php环境中工作,并使使用..,PHP类和函数:)。

更重要的是,你链接到的文件是一个简单的功能。一个功能。你知道你可以做什么?按照原样取出文件,将其命名为creditcard_helper.php,将其放入helpers文件夹中,将其打开并将整个代码放入此片段中(丑陋但必要,因为无论何时您再次加载助手,它都会给出错误):

if(!function_exists('checkCreditCard') 
{ 
    //the whole content goes here untouched; 
} 

然后你就定了。只需使用:

$this->load->helper('creditcard'); 
if(checkCreditCard($cardnumber, $cardname, &$errornumber, &$errortext)) 
{ 
    echo 'card OK'; 
} 
else 
{ 
echo 'wrong card type/number'; 
}