信用卡安全验证代码php
时间:2007-02-17 来源:PHP爱好者
信用卡安全验证代码
关键词:PHP
本例通过MOD10算法来验证信用卡的合法性。可用于电子商务等领域。
/* This code has been implemented by Julien Buratto [email protected] */
/* It's free to use to anybody, for any use (commercial or whatever) */
/* Please edit/modify this file to your needs but do NOT remove my name */
/* from it :-) */
/* I would be pleased if U would mail me in the case U like it */
/* I will never ask money for it, so don't let people ask money for it*/
/* It can validate Amex,Visa (13 and 16 digits) and Novus card */
/* As all MOD10 algorithm I give NO warranty to it */
/* However should be OK for 90% of credit cards */
/* For PHP3 programmers: remove the first if condition if U have your own*/
/* form to pass the card number: the card number to validate must be in the */
/* $carta variable. */
/* EXPLANATIONS */
$date=time(); /* get current date */
/* Get this year and this month and put them in variables */
$thisyear=date("Y",$date);
$thismonth=date("m",$date);
/* if the card is void then make the form */
if ($carta==0){
?>
Card Number:
Expiration date:
/* If card number is not void then try to validate it */
if ($carta!=0){
/* if the month of exp is passed,this year, card is expired */
/* Remember that the card will never be a year in the past as the year list */
/* has been created starting from $thisyear (see next)*/
if($month<$thismonth and $year==$thisyear) exit("Card has expired!");
$cclen=strlen($carta);
/* Check card number lenght: must be 15 or 13 or 16 */
if ($cclen!=13 and $cclen!=15 and $cclen!=16) exit("Credit card not valid: check
spelling.");
/* Check if the card has a pair or odd number of digits */
/* the % give the rest of a division, like: 5 divided 2 -> give 1 */
if ($cclen%2==0) {
$p=0; } /* PAIR */
else {$p=2;} /* ODD */
/* Get each number of the card and put it in an array */
for($a=0;$a!=$cclen;$a++){
$X[$a]=substr($carta,$a,1);
}
/* Algorithm Mod10 modificated (by me :-) */
for ($nume=($p/2);$nume!=$cclen-$p;$nume=$nume+2){
$X[$nume]=$X[$nume]*2;
if ($X[$nume]>=10) {
$X1=substr($X[$nume],0,1);
$X2=substr($X[$nume],1,1);
$X[$nume]=substr($X[$nume],0,1)+substr($X[$nume],1,1);
}
}
/* Sums each value of the modificated array */
/* Check the result of the algorithm */
for ($i=0;$i!=$cclen;$i++){
$val=$val+$X[$i];
}
/* Please note: if the second number of the result of algorithm is not 0 */
/* then the card is NOT valid */
if ($val==0) exit("Credit Card not valid: check spelling");
if (substr($val,1,1)!=0){ $valida="not";}
echo ("The card: $carta seems to be: $valida valid");
exit();
}
/* This condition in the case $card contains characters */
exit("Maybe the Card Number inserted contains characters ? Only numbers
please!");
?>
php爱好者站 http://www.phpfans.net php基础|php进阶|php模板.
关键词:PHP
本例通过MOD10算法来验证信用卡的合法性。可用于电子商务等领域。
/* This code has been implemented by Julien Buratto [email protected] */
/* It's free to use to anybody, for any use (commercial or whatever) */
/* Please edit/modify this file to your needs but do NOT remove my name */
/* from it :-) */
/* I would be pleased if U would mail me in the case U like it */
/* I will never ask money for it, so don't let people ask money for it*/
/* It can validate Amex,Visa (13 and 16 digits) and Novus card */
/* As all MOD10 algorithm I give NO warranty to it */
/* However should be OK for 90% of credit cards */
/* For PHP3 programmers: remove the first if condition if U have your own*/
/* form to pass the card number: the card number to validate must be in the */
/* $carta variable. */
/* EXPLANATIONS */
$date=time(); /* get current date */
/* Get this year and this month and put them in variables */
$thisyear=date("Y",$date);
$thismonth=date("m",$date);
/* if the card is void then make the form */
if ($carta==0){
?>
Card Number:
Expiration date:
/* If card number is not void then try to validate it */
if ($carta!=0){
/* if the month of exp is passed,this year, card is expired */
/* Remember that the card will never be a year in the past as the year list */
/* has been created starting from $thisyear (see next)*/
if($month<$thismonth and $year==$thisyear) exit("Card has expired!");
$cclen=strlen($carta);
/* Check card number lenght: must be 15 or 13 or 16 */
if ($cclen!=13 and $cclen!=15 and $cclen!=16) exit("Credit card not valid: check
spelling.");
/* Check if the card has a pair or odd number of digits */
/* the % give the rest of a division, like: 5 divided 2 -> give 1 */
if ($cclen%2==0) {
$p=0; } /* PAIR */
else {$p=2;} /* ODD */
/* Get each number of the card and put it in an array */
for($a=0;$a!=$cclen;$a++){
$X[$a]=substr($carta,$a,1);
}
/* Algorithm Mod10 modificated (by me :-) */
for ($nume=($p/2);$nume!=$cclen-$p;$nume=$nume+2){
$X[$nume]=$X[$nume]*2;
if ($X[$nume]>=10) {
$X1=substr($X[$nume],0,1);
$X2=substr($X[$nume],1,1);
$X[$nume]=substr($X[$nume],0,1)+substr($X[$nume],1,1);
}
}
/* Sums each value of the modificated array */
/* Check the result of the algorithm */
for ($i=0;$i!=$cclen;$i++){
$val=$val+$X[$i];
}
/* Please note: if the second number of the result of algorithm is not 0 */
/* then the card is NOT valid */
if ($val==0) exit("Credit Card not valid: check spelling");
if (substr($val,1,1)!=0){ $valida="not";}
echo ("The card: $carta seems to be: $valida valid");
exit();
}
/* This condition in the case $card contains characters */
exit("Maybe the Card Number inserted contains characters ? Only numbers
please!");
?>
php爱好者站 http://www.phpfans.net php基础|php进阶|php模板.
相关阅读 更多 +