欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php实现通用的信用卡验证类

程序员文章站 2022-07-26 12:22:05
本文实例讲述了php实现通用的信用卡验证类。分享给大家供大家参考。 原文说明如下: credit card validation solution (php editi...

本文实例讲述了php实现通用的信用卡验证类。分享给大家供大家参考。

原文说明如下:

credit card validation solution (php edition)
version 3.5

description
credit card validation solution™ uses a four step process to ensure credit card numbers are keyed in correctly. this procedure accurately checks cards from american express, australian bankcard, carte blache, diners club, discover/novus, jcb, mastercard and visa.
for more information, please read the comments in the code itself.

installation instructions
select the text between the two lines indicated, below.
copy the text.
open up a text editor.
paste the text.
save that file. when saving it, make sure to:
save it in a directory on your webserver, and
name it with an extension that your server will recognize needs parsing by php.
to see it in action, open up that file in your web browswer.

具体代码如下:

<?php
# ------------------------------------------------------------------------
# credit card validation solution, version 3.5         php edition
# 25 may 2000
#
# copyright notice:
# a) this code is property of the analysis and solutions company.
# b) it is being distributed free of charge and on an "as is" basis.
# c) use of this code, or any part thereof, is contingent upon leaving
#   this copyright notice, name and address information in tact.
# d) written permission must be obtained from us before this code, or any
#   part thereof, is sold or used in a product which is sold.
# e) by using this code, you accept full responsibility for its use
#   and will not hold the analysis and solutions company, its employees
#   or officers liable for damages of any sort.
# f) this code is not to be used for illegal purposes.
# g) please email us any revisions made to this code.
#
# copyright 2000         http://www.analysisandsolutions.com/code/
# the analysis and solutions company     info@analysisandsolutions.com
# ------------------------------------------------------------------------
#
# description:
# credit card validation solution uses a four step process to ensure
# credit card numbers are keyed in correctly. this procedure accurately
# checks cards from american express, australian bankcard, carte blache,
# diners club, discover/novus, jcb, mastercard and visa.
#
# caution:
# ccvs uses exact number ranges as part of the validation process. these
# ranges are current as of 20 october 1999. if presently undefined ranges
# come into use in the future, this program will improperly deject card
# numbers in such ranges, rendering an error message entitled "potential
# card type discrepancy." if this happens while entering a card & type
# you know are valid, please contact us so we can update the ranges.
#
# potential customizations:
# * if you don't accept some of these card types, edit step 2, using pound
# signs "#" to comment out the "elseif," "$cardname" and "$shouldlength"
# lines in question.
# * additional card types can be added by inserting new "elseif,"
# "$cardname" and "$shouldlength" lines in step 2.
# * the three functions here can be called by other php documents to check
# any number.
#
# credits:
# we learned of the mod 10 algorithm in some perl code, entitled
# "the validator," available on matt's script archive,
# http://worldwidemart.com/scripts/readme/ccver.shtml. that code was
# written by david paris, who based it on material melvyn myers reposted
# from an unknown author. paris credits aries solis for tracking down the
# data underlying the algorithm. at the same time, our code bears no
# resemblance to its predecessors. ccvalidationsolution was first written
# for visual basic, on which allen browne and rico zschau assisted.
# neil fraser helped prune down the onlynumericsolution() for perl.
function ccvalidationsolution ($number) {
  global $cardname;
  # 1) get rid of spaces and non-numeric characters.
  $number = onlynumericsolution($number);
  # 2) do the first four digits fit within proper ranges?
  #   if so, who's the card issuer and how long should the number be?
  $numberleft = substr($number, 0, 4);
  $numberlength = strlen($number);
  if ($numberleft >= 3000 and $numberleft <= 3059) {
    $cardname = "diners club";
    $shouldlength = 14;
  } elseif ($numberleft >= 3600 and $numberleft <= 3699) {
    $cardname = "diners club";
    $shouldlength = 14;
  } elseif ($numberleft >= 3800 and $numberleft <= 3889) {
    $cardname = "diners club";
    $shouldlength = 14;
  } elseif ($numberleft >= 3400 and $numberleft <= 3499) {
    $cardname = "american express";
    $shouldlength = 15;
  } elseif ($numberleft >= 3700 and $numberleft <= 3799) {
    $cardname = "american express";
    $shouldlength = 15;
  } elseif ($numberleft >= 3528 and $numberleft <= 3589) {
    $cardname = "jcb";
    $shouldlength = 16;
  } elseif ($numberleft >= 3890 and $numberleft <= 3899) {
    $cardname = "carte blache";
    $shouldlength = 14;
  } elseif ($numberleft >= 4000 and $numberleft <= 4999) {
    $cardname = "visa";
    if ($numberlength > 14) {
      $shouldlength = 16;
    } elseif ($numberlength < 14) {
      $shouldlength = 13;
    } else {
      echo "<br /><em>the visa number entered, $number, in is 14 digits long.<br />visa cards usually have 16 digits, though some have 13.<br />please check the number and try again.</em><br />n";
      return false;
    }
  } elseif ($numberleft >= 5100 and $numberleft <= 5599) {
    $cardname = "mastercard";
    $shouldlength = 16;
  } elseif ($numberleft == 5610) {
    $cardname = "australian bankcard";
    $shouldlength = 16;
  } elseif ($numberleft == 6011) {
    $cardname = "discover/novus";
    $shouldlength = 16;
  } else {
    echo "<br /><em>the first four digits of the number entered are $numberleft. <br />if that's correct, we don't accept that type of credit card.<br />if it's wrong, please try again.</em><br />n";
    return false;
  }
  # 3) is the number the right length?
  if ($numberlength <> $shouldlength) {
    $missing = $numberlength - $shouldlength;
    if ($missing < 0) {
      echo "<br /><em>the $cardname number entered, $number, is missing " . abs($missing) . " digit(s).<br />please check the number and try again.</em><br />n";
    } else {
      echo "<br /><em>the $cardname number entered, $number, has $missing too many digit(s).<br />please check the number and try again.</em><br />n";
    }
    return false;
  }
  # 4) does the number pass the mod 10 algorithm checksum?
  if (mod10solution($number) == true) {
    return true;
  } else {
    echo "<br /><em>the $cardname number entered, $number, is invalid.<br />please check the number and try again.</em><br />n";
  return false;
  }
}
function onlynumericsolution ($number) {
  # remove any non numeric characters.
  # ensure number is no more than 19 characters long.
  return substr( ereg_replace( "[^0-9]", "", $number) , 0, 19);
}
function mod10solution ($number) {
  $numberlength = strlen($number);
  $checksum = 0;
  # add even digits in even length strings
  # or odd digits in odd length strings.
  for ($location = 1 - ($numberlength % 2); $location < $numberlength; $location += 2) {
    $checksum += substr($number, $location, 1);
  }
  # analyze odd digits in even length strings
  # or even digits in odd length strings.
  for ($location = ($numberlength % 2); $location < $numberlength; $location += 2) {
    $digit = substr($number, $location, 1) * 2;
    if ($digit < 10) {
      $checksum += $digit;
    } else {
      $checksum += $digit - 9;
    }
  }
  # is the checksum divisible by ten?
  return ($checksum % 10 == 0);
}
# ----------- begin sample user interface section ------------
#
# this section provides a simple sample user interface for the
# credit card validation functions. it generates an html form
# where you enter a card number to check.
#
  # if a number has been posted by the form, check it.
  if ( isset($number) ) {
    # get rid of spaces and non-numeric characters in posted
    # numbers so they display correctly on the input form.
    $number = onlynumericsolution($number);
    if (ccvalidationsolution($number) == true) {
      echo "<br />the $cardname number entered, $number, <em>is</em> valid.<br />n";
    }
  } else {
    $number = "";
  }
  # setup an input form. posting it calls this page again.
  echo "<form method="post" action="$request_uri">n";
  echo "<br />credit card number: <input type="text" name="number" value="$number">n";
  echo "<input type="submit" name="submitr" value="check its validity">n";
  echo "</form><br />n";
#
# ------------ end sample user interface section -------------
?>

希望本文所述对大家的php程序设计有所帮助。