我有一个PHP验证器,它返回true,但我需要添加false

时间:2013-05-27 23:37:22

标签: php validation

我需要帮助这个代码,是一个信用卡验证,我一直试图让它在我的代码中工作,但我不能,我只需要获得2个变量,是卡正在工作它应该返回true工作,但如果不是它显示错误信息,但我还需要获得一个返回false,这里是代码:

    <?php
    $cardnumber = $_POST['CardNumber']; 
    $cardname = $_POST['CardType'];

    $cards = array (  array ('name' => 'American Express', 
    'length' => '15', 
    'prefixes' => '34,37',
    'checkdigit' => true
    ),
    array ('name' => 'Diners Club Carte Blanche', 
    'length' => '14', 
    'prefixes' => '300,301,302,303,304,305',
    'checkdigit' => true
    ),
    array ('name' => 'Diners Club', 
    'length' => '14,16',
    'prefixes' => '36,38,54,55',
    'checkdigit' => true
    ),
    array ('name' => 'Discover', 
    'length' => '16', 
    'prefixes' => '6011,622,64,65',
    'checkdigit' => true
    ),
    array ('name' => 'Diners Club Enroute', 
    'length' => '15', 
    'prefixes' => '2014,2149',
    'checkdigit' => true
    ),
    array ('name' => 'JCB', 
    'length' => '16', 
    'prefixes' => '35',
    'checkdigit' => true
    ),
    array ('name' => 'Maestro', 
    'length' => '12,13,14,15,16,18,19', 
    'prefixes' => '5018,5020,5038,6304,6759,6761,6762,6763',
    'checkdigit' => true
    ),
                       array ('name' => 'MasterCard', 
    'length' => '16', 
    'prefixes' => '51,52,53,54,55',
    'checkdigit' => true
    ),
    array ('name' => 'Solo', 
    'length' => '16,18,19', 
    'prefixes' => '6334,6767',
    'checkdigit' => true
    ),
                       array ('name' => 'Switch', 
    'length' => '16,18,19', 
    'prefixes' => '4903,4905,4911,4936,564182,633110,6333,6759',
    'checkdigit' => true
    ),
    array ('name' => 'VISA', 
    'length' => '16', 
    'prefixes' => '4',
    'checkdigit' => true
    ),
    array ('name' => 'VISA Electron', 
    'length' => '16', 
    'prefixes' => '417500,4917,4913,4508,4844',
    'checkdigit' => true
    ),
    array ('name' => 'LaserCard', 
    'length' => '16,17,18,19', 
    'prefixes' => '6304,6706,6771,6709',
    'checkdigit' => true
    )
    );

      $ccErrorNo = 0;

      $ccErrors [0] = "Unknown card type";
      $ccErrors [1] = "No card number provided";
      $ccErrors [2] = "Credit card number has invalid format";
      $ccErrors [3] = "Credit card number is invalid";
      $ccErrors [4] = "Credit card number is wrong length";

      // Establish card type
      $cardType = -1;
      for ($i=0; $i<sizeof($cards); $i++) {

        // See if it is this card (ignoring the case of the string)
        if (strtolower($cardname) == strtolower($cards[$i]['name'])) {
          $cardType = $i;
          break;
        }
      }

      // If card type not found, report an error
      if ($cardType == -1) {
         $errornumber = 0;     
         $errortext = $ccErrors [$errornumber];
         return true; 
      }

      // Ensure that the user has provided a credit card number
      if (strlen($cardnumber) == 0)  {
         $errornumber = 1;     
         $errortext = $ccErrors [$errornumber];
         return true; 
      }

      // Remove any spaces from the credit card number
      $cardNo = str_replace (' ', '', $cardnumber);  

      // Check that the number is numeric and of the right sort of length.
      if (!preg_match("/^[0-9]{13,19}$/",$cardNo))  {
         $errornumber = 2;     
         $errortext = $ccErrors [$errornumber];
         return true; 
      }

      // Now check the modulus 10 check digit - if required
      if ($cards[$cardType]['checkdigit']) {
        $checksum = 0;        // running checksum total
        $mychar = "";         // next char to process
        $j = 1;               // takes value of 1 or 2

        // Process each digit one by one starting at the right
        for ($i = strlen($cardNo) - 1; $i >= 0; $i--) {

          // Extract the next digit and multiply by 1 or 2 on alternative digits.      
          $calc = $cardNo{$i} * $j;

          // If the result is in two digits add 1 to the checksum total
          if ($calc > 9) {
            $checksum = $checksum + 1;
            $calc = $calc - 10;
          }

          // Add the units element to the checksum total
          $checksum = $checksum + $calc;

          // Switch the value of j
          if ($j ==1) {$j = 2;} else {$j = 1;};
        } 

        // All done - if checksum is divisible by 10, it is a valid modulus 10.
        // If not, report an error.
        if ($checksum % 10 != 0) {
         $errornumber = 3;     
         $errortext = $ccErrors [$errornumber];
         return true; 
        }
      }  

      // The following are the card-specific checks we undertake.

      // Load an array with the valid prefixes for this card
      $prefix = explode(',',$cards[$cardType]['prefixes']);

      // Now see if any of them match what we have in the card number  
      $PrefixValid = false; 
      for ($i=0; $i<sizeof($prefix); $i++) {
        $exp = '/^' . $prefix[$i] . '/';
        if (preg_match($exp,$cardNo)) {
          $PrefixValid = true;
          break;
        }
      }

      // If it isn't a valid prefix there's no point at looking at the length
      if (!$PrefixValid) {
         $errornumber = 3;     
         $errortext = $ccErrors [$errornumber];
         return true; 
      }

      // See if the length is valid for this card
      $LengthValid = false;
      $lengths = explode(',',$cards[$cardType]['length']);
      for ($j=0; $j<sizeof($lengths); $j++) {
        if (strlen($cardNo) == $lengths[$j]) {
          $LengthValid = true;
          break;
        }
      }

      // See if all is OK by seeing if the length was valid. 
      if (!$LengthValid) {
         $errornumber = 4;     
         $errortext = $ccErrors [$errornumber];
         return true; 
      };   

      // The credit card is in the required format.
      return true;

echo $errortext;
?>

你可以看到返回true,但是我还需要在卡未经过验证时将其更改为false,这样我可以进行其他验证,感谢您的帮助。

我发现其他代码可以做我需要的,更简单一点,是否可以将这个新代码的$ verified变量添加到第一个?这是代码:

<?php

$cc_number = $_POST['CardNumber']; 
$type = $_POST['CardType'];

$cc_num = str_replace (' ', '', $cc_number);  

    if($type == "AX") {
    $denum = "American Express";
    } elseif($type == "DC") {
    $denum = "Diner's Club";
    } elseif($type == "DS") {
    $denum = "Discover";
    } elseif($type == "MC") {
    $denum = "Master Card";
    } elseif($type == "VI") {
    $denum = "Visa";
    }

    if($type == "AX") {
    $pattern = "/^([34|37]{2})([0-9]{13})$/";//American Express
    if (preg_match($pattern,$cc_num)) {
    $verified = true;
    } else {
    $verified = false;
    }


    } elseif($type == "DC") {
    $pattern = "/^([30|36|38]{2})([0-9]{12})$/";//Diner's Club
    if (preg_match($pattern,$cc_num)) {
    $verified = true;
    } else {
    $verified = false;
    }


    } elseif($type == "DS") {
    $pattern = "/^([6011]{4})([0-9]{12})$/";//Discover Card
    if (preg_match($pattern,$cc_num)) {
    $verified = true;
    } else {
    $verified = false;
    }


    } elseif($type == "MC") {
    $pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/";//Mastercard
    if (preg_match($pattern,$cc_num)) {
    $verified = true;
    } else {
    $verified = false;
    }


    } elseif($type == "VI") {
    $pattern = "/^([4]{1})([0-9]{12,15})$/";//Visa
    if (preg_match($pattern,$cc_num)) {
    $verified = true;
    } else {
    $verified = false;
    }

    }

    if($verified == false) {
    //Do something here in case the validation fails
    echo "Credit card invalid. Please make sure that you entered a valid <em>" . $denum . "</em> credit card ";

    } else { //if it will pass...do something
    echo "Your <em>" . $denum . "</em> credit card is valid";
    }

?>

3 个答案:

答案 0 :(得分:1)

在函数顶部声明一个变量:

$invalid = false;

然后在代码中的任何地方return true;将其更改为:$invalid = true;

然后你可以在你的功能结束时做return $invalid;。如果出现错误,它将返回true。如果没有错误,它将返回false

答案 1 :(得分:0)

查看所有if语句,如下所示:

$errornumber = whateverNumber;
$errortext = $ccErrors [$errornumber];
return true;

出现错误时返回true。只需将return语句更改为这些位置的return false;即可使脚本返回false。

唯一的return true;应该在你的功能结束时。

答案 2 :(得分:0)

感谢您的帮助,这是最终的代码,我只是进行了一些更改并且效果很好:

  $cards = array (  array ('name' => 'American Express', 
                          'length' => '15', 
                          'prefixes' => '34,37',
                          'checkdigit' => true
                         ),
                   array ('name' => 'Diners Club Carte Blanche', 
                          'length' => '14', 
                          'prefixes' => '300,301,302,303,304,305',
                          'checkdigit' => true
                         ),
                   array ('name' => 'Diners Club', 
                          'length' => '14,16',
                          'prefixes' => '36,38,54,55',
                          'checkdigit' => true
                         ),
                   array ('name' => 'Discover', 
                          'length' => '16', 
                          'prefixes' => '6011,622,64,65',
                          'checkdigit' => true
                         ),
                   array ('name' => 'Diners Club Enroute', 
                          'length' => '15', 
                          'prefixes' => '2014,2149',
                          'checkdigit' => true
                         ),
                   array ('name' => 'JCB', 
                          'length' => '16', 
                          'prefixes' => '35',
                          'checkdigit' => true
                         ),
                   array ('name' => 'Maestro', 
                          'length' => '12,13,14,15,16,18,19', 
                          'prefixes' => '5018,5020,5038,6304,6759,6761,6762,6763',
                          'checkdigit' => true
                         ),
                   array ('name' => 'MasterCard', 
                          'length' => '16', 
                          'prefixes' => '51,52,53,54,55',
                          'checkdigit' => true
                         ),
                   array ('name' => 'Solo', 
                          'length' => '16,18,19', 
                          'prefixes' => '6334,6767',
                          'checkdigit' => true
                         ),
                   array ('name' => 'Switch', 
                          'length' => '16,18,19', 
                          'prefixes' => '4903,4905,4911,4936,564182,633110,6333,6759',
                          'checkdigit' => true
                         ),
                   array ('name' => 'VISA', 
                          'length' => '16', 
                          'prefixes' => '4',
                          'checkdigit' => true
                         ),
                   array ('name' => 'VISA Electron', 
                          'length' => '16', 
                          'prefixes' => '417500,4917,4913,4508,4844',
                          'checkdigit' => true
                         ),
                   array ('name' => 'LaserCard', 
                          'length' => '16,17,18,19', 
                          'prefixes' => '6304,6706,6771,6709',
                          'checkdigit' => true
                         )
                );

  $ccErrorNo = 0;

  $ccErrors [0] = "Unknown card type";
  $ccErrors [1] = "No card number provided";
  $ccErrors [2] = "Credit card number has invalid format";
  $ccErrors [3] = "Credit card number is invalid";
  $ccErrors [4] = "Credit card number is wrong length";

  // Establish card type
  $cardType = -1;
  for ($i=0; $i<sizeof($cards); $i++) {

    // See if it is this card (ignoring the case of the string)
    if (strtolower($cardname) == strtolower($cards[$i]['name'])) {
      $cardType = $i;
      $response = true;
      break;
    }
  }

  // If card type not found, report an error
  if ($cardType == -1) {
     $errornumber = 0;     
     $errortext = $ccErrors [$errornumber];
     $response = false;
     return false; 
  }

  // Ensure that the user has provided a credit card number
  if (strlen($cardnumber) == 0)  {
     $errornumber = 1;     
     $errortext = $ccErrors [$errornumber];
     $response = false;
     return false; 
  }

  // Remove any spaces from the credit card number
  $cardNo = str_replace (' ', '', $cardnumber);  

  // Check that the number is numeric and of the right sort of length.
  if (!preg_match("/^[0-9]{13,19}$/",$cardNo))  {
     $errornumber = 2;     
     $errortext = $ccErrors [$errornumber];
     $response = false;
     return false; 
  }

  // Now check the modulus 10 check digit - if required
  if ($cards[$cardType]['checkdigit']) {
    $checksum = 0;                                  // running checksum total
    $mychar = "";                                   // next char to process
    $j = 1;                                         // takes value of 1 or 2

    // Process each digit one by one starting at the right
    for ($i = strlen($cardNo) - 1; $i >= 0; $i--) {

      // Extract the next digit and multiply by 1 or 2 on alternative digits.      
      $calc = $cardNo{$i} * $j;

      // If the result is in two digits add 1 to the checksum total
      if ($calc > 9) {
        $checksum = $checksum + 1;
        $calc = $calc - 10;
      }

      // Add the units element to the checksum total
      $checksum = $checksum + $calc;

      // Switch the value of j
      if ($j ==1) {$j = 2;} else {$j = 1;};
    } 

    // All done - if checksum is divisible by 10, it is a valid modulus 10.
    // If not, report an error.
    if ($checksum % 10 != 0) {
     $errornumber = 3;     
     $errortext = $ccErrors [$errornumber];
     $response = false;
     return false; 
    }
  }  

  // The following are the card-specific checks we undertake.

  // Load an array with the valid prefixes for this card
  $prefix = explode(',',$cards[$cardType]['prefixes']);

  // Now see if any of them match what we have in the card number  
  $PrefixValid = false; 
  for ($i=0; $i<sizeof($prefix); $i++) {
    $exp = '/^' . $prefix[$i] . '/';
    if (preg_match($exp,$cardNo)) {
      $PrefixValid = true;
      $response = true;
      break;
    }
  }

  // If it isn't a valid prefix there's no point at looking at the length
  if (!$PrefixValid) {
     $errornumber = 3;     
     $errortext = $ccErrors [$errornumber];
     $response = false;
     return false; 
  }

  // See if the length is valid for this card
  $LengthValid = false;
  $lengths = explode(',',$cards[$cardType]['length']);
  for ($j=0; $j<sizeof($lengths); $j++) {
    if (strlen($cardNo) == $lengths[$j]) {
      $LengthValid = true;
      $response = true;
      break;
    }
  }

  // See if all is OK by seeing if the length was valid. 
  if (!$LengthValid) {
     $errornumber = 4;     
     $errortext = $ccErrors [$errornumber];
     $response = false;
     return false; 
  };   

  // The credit card is in the required format.
  return $response;

在另一页:

$cardnumber = $_POST['CardNumber']; 
$cardname = $_POST['CardType'];

require('phpcreditcard.php');



  if ($response == true) {
      echo 'works';
  }
  elseif ($response == false) {
    echo $errortext;
  }