检查信用卡号是否有效时收到警告

时间:2013-01-03 07:07:04

标签: codeigniter validation credit-card

当我检查信用卡号是否有效时收到此警告。使用语句if(card_number_valid($this->input->post('card_number'))!=1)

调用creditcard_helper.php
A PHP Error was encountered

Severity: 8192

Message: Function ereg_replace() is deprecated

Filename: helpers/creditcard_helper.php

Line Number: 51

creditcard_helper.php

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* Credit Card Functions
*
* This helper module contains functions which can be used to manipulate credit
* card numbers and related information.
*
* @package    CodeIgniter
* @subpackage    Helpers
* @category    Helpers
* @author    Jim O'Halloran
*/


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

2 个答案:

答案 0 :(得分:1)

请使用

preg_replace()

而不是

ereg_replace()

自PHP 5.3.0起,此功能已被弃用。

答案 1 :(得分:0)

根据您发布的内容,这不是发生的实际错误,而是一条警告消息,除非您的配置设置为警告错误。

See the ereg_replace documentation.