【问题标题】:What is the best way to validate a credit card in codeigniter在codeigniter中验证信用卡的最佳方法是什么
【发布时间】:2012-01-19 01:23:50
【问题描述】:

嗨,我正在使用 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 中是否有任何库或助手。请帮忙........

【问题讨论】:

  • 您是否考虑过将链接到的代码移植到 CI 插件?
  • @Jared Farrish,还没有,我想知道 codeigniter 中是否有任何现有的。如果没有,我会将其移植到 codeigniter
  • @ajreal,我想要一个 codeigniter 解决方案。 PHP 解决方案无济于事
  • @Kanishka Panamaldeniya - CodeIgniter 解决方案与 PHP 解决方案有何不同? CodeIgniter 助手只是普通的 PHP 函数!!

标签: php codeigniter credit-card


【解决方案1】:

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

更重要的是,您链接到的文件是一个简单的函数。一功能。你知道你能做什么吗?按原样获取文件,将其命名为 creditcard_helper.php,将其放入 helpers 文件夹中,打开它并将整个代码放入此 sn-p 中(丑陋但必要,因为每当您第二次加载帮助程序时,它都会否则给你错误):

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';
}

【讨论】:

    【解决方案2】:

    我在CodeIgniter Github wiki page 中找到了this helper。当放入 helpers 文件夹时,您可以在控制器或模型中使用文件中的函数。

    【讨论】:

      猜你喜欢
      • 2010-09-15
      • 2015-09-14
      • 2021-01-02
      • 2018-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多