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

本文实例讲述了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程序设计有所帮助。

(0)

相关推荐

  • PHP 芝麻信用接入的注意事项

    芝麻官方下载的SDK,跑不起来,百度搜索一番也没有发现太多的文章 ,只有一个CSDN博客写的一篇文章,比较有参考价值 详细查阅文档+几天测试整理以下几点注意事项: 接入芝麻API接口,应该分2步: 第一步,获得授权,这个,官方的SDK,只要吧参数配置正确,就能操作. 注意下面这些参数,和 芝麻文档对比, 另外最重要的一点,,,芝麻公钥生成,以及修改,生成的公钥按照官方的操作复制进芝麻后台,保存之后,芝麻会生成新的公钥,点击复制,在保存到我们的公钥文件,然后上传到空间给PHP调用. $reques

  • php实现信用卡校验位算法THE LUHN MOD-10示例

    按照ISO 2894中支付卡校验位的算法 The Luhn Mod-10 Method 规定: 1.对卡号上的每位数字乘以权重.其规则是,如果卡号数字个数是偶数,则第一位乘以2,否则就乘以1,然后以后分别是,1,2,1,2,1,2;2.如果每位数字乘以权重后超过9 ,则需要减去 9;3.将所有的处理过的加权数字求和,用 数字 10 求模运算;4.余数应该是0,否则可能是输入错误.也可能是一个假号. 顺手PHP简单实现下,实际场景前端验证好一些,例如JS. 复制代码 代码如下: function

  • PHP随机生成信用卡卡号的方法

    本文实例讲述了PHP随机生成信用卡卡号的方法.分享给大家供大家参考.具体分析如下: 这段PHP代码根据信用卡卡号产生规则随机生成信用卡卡号,是可以通过验证的,仅供学习参考,请不要用于非法用途,否则后果自负. <?php /* PHP credit card number generator Copyright (C) 2006 Graham King graham@darkcoding.net This program is free software; you can redistribute

  • PHP实现通过Luhn算法校验信用卡卡号是否有效

    本文实例讲述了PHP实现通过Luhn算法校验信用卡卡号是否有效的方法.分享给大家供大家参考.具体实现方法如下: $numbers = "49927398716 49927398717 1234567812345678 1234567812345670"; foreach (split(' ', $numbers) as $n) echo "$n is ", luhnTest($n) ? 'valid' : 'not valid', '</br>'; fu

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

    本文实例讲述了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

  • JS实现的通用表单验证插件完整实例

    本文实例讲述了JS实现的通用表单验证插件.分享给大家供大家参考.具体如下: 这里演示一个通用的JS表单验证插件代码.使用方法:第一步:需设定表单项数据类型,第二步:实例表单验证,验证错误提示说明(程序有自带相关错误提示,可自定义每项验证错误提示时文本,只需添加msg). 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/js-normal-table-check-plug-codes/ 具体代码如下: <!DOCTYPE html PUBLIC &qu

  • Jquery 表单验证类介绍与实例

    [html] 复制代码 代码如下: <form action="" method="post" id="formValidate"> 数值:<input name="" type="text" validate="number" /><span></span><br/> 浮点型:<input name="&quo

  • Vue快速实现通用表单验证功能

    本文开篇第一句话,想引用鲁迅先生<祝福>里的一句话,那便是:"我真傻,真的,我单单知道后端整天都是CRUD,我没想到前端整天都是Form表单".这句话要从哪里说起呢?大概要从最近半个月的"全栈工程师"说起.项目上需要做一个城市配载的功能,顾名思义,就是通过框选和拖拽的方式在地图上完成配载.博主选择了前后端分离的方式,在这个过程中发现:首先,只要有依赖jQuery的组件,譬如Kendoui,即使使用了Vue,依然需要通过jQuery去操作DOM.其次,只有

  • PHP代码实现表单数据验证类

    下面通过一段PHP代码实现表单数据验证类,具体介绍如下: 非常好用方便的表单数据验证类 <?php //验证类 class Fun{ function isEmpty($val) { if (!is_string($val)) return false; //是否是字符串类型 if (empty($val)) return false; //是否已设定 if ($val=='') return false; //是否为空 return true; } /* -------------------

  • C#自定义RSA加密解密及RSA签名和验证类实例

    本文实例讲述了C#自定义RSA加密解密及RSA签名和验证类.分享给大家供大家参考.具体分析如下: 这个C#类自定义RSA加密解密及RSA签名和验证,包含了RSA加密.解密及签名所需的相关函数,带有详细的注释说明. using System; using System.Text; using System.Security.Cryptography; namespace DotNet.Utilities { /// <summary> /// RSA加密解密及RSA签名和验证 /// </

  • php可扩展的验证类实例(可对邮件、手机号、URL等验证)

    本文实例讲述了php可扩展的验证类.分享给大家供大家参考.具体分析如下: 这里介绍一个可扩展的php验证类, 类里面可以的各类验证可自行调整实现,现在为基本实现方式. 需要添加规则的话, 直接定义方法,方法名即为规则名称.具体参考使用方法. require_once('./Validator.class.php'); $data = array( 'nickname' => 'heno' , 'realname' => 'steven', 'age' => 25, 'mobile' =&

  • php封装的表单验证类完整实例

    本文实例讲述了php封装的表单验证类.分享给大家供大家参考,具体如下: <?php //封装一个表单验证类 //中文验证.邮箱验证.电话号码.手机.QQ.身份证.(由字母.数字.下划线组成,不能以数字开头) header('content-type:text/html;charset=utf-8'); class Form{ /* //中文验证的方法 //参数:$str,$num1,$num2 //返回值:匹配成功返回匹配的次数 */ public function checkChina($st

  • php常用表单验证类用法实例

    本文实例讲述了php常用表单验证类用法.分享给大家供大家参考.具体如下: <?php /** * 页面作用:常用表单验证类 * 作 者:欣然随风 * QQ:276624915 */ class class_post { //验证是否为指定长度的字母/数字组合 function fun_text1($num1,$num2,$str) { Return (preg_match("/^[a-zA-Z0-9]{".$num1.",".$num2."}$/&q

  • js番茄的表单验证类

    相信大家看代码后再添加一些针对其他格式的验证也不是太麻烦的事情 对于各种提示效果需要修改 feedback 方法就可以实现,因为不想让代码变得太臃肿,所以有很多不常用的功能就没有再添加了 主要目的提供大家参考与分享,也希望能够根据大家的意见使其变得更加完善 番茄的表单验证类_我们 //去除字符串两边的空格 String.prototype.trim = function () { return this.replace(/(^\s+)|(\s+$)/g, ""); } //检测字符串是

随机推荐