Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

credit card number verification 1

Status
Not open for further replies.

DeepBlerg

Technical User
Jan 13, 2001
224
AU
Hi,

I'm setting up an online shop and have heard that PHP supports basic credit card number verification (ie. mod-10, if the credit card number is valid). Has anyone used this before?

Thanks.
 
something for you.

function checkCC($a){
$count=0;
for($i=strlen($a)-1; $i>=0;$i--){
$count+=(((strlen($a)-$i-1)%2)==0) ? $a[$i] : (floor($a[$i]*2/10)+($a[$i]*2%10));
}
return (($count%10)==0) ? true : false;
}
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Anikin's code will check the luhn-10 checksum of a credit-card number presented. Use it.

I recommend you also perform other other checks, too, based on the type of card. For example, it is possible to input a supposed Visa number that passes luhn-10, but does not begin with a "4", as all Visa card numbers must.

Here's the guts of a switch statement I have used in my code before:

[tt]
$cc_good = FALSE;

switch ($cc_brand)
{
case "MasterCard" :
$cc_good = preg_match ("/^5[1-5]\d{14}$/", $cc_num);
break;

case "Visa" :
$cc_good = preg_match ("/^4\d{15}$|^4\d{12}$/", $cc_num);
break;

case "American Express" :
$cc_good = preg_match ("/^3[47]\d{13}$/", $cc_num);
break;

case "Discover" :
$cc_good = preg_match ("/^6011\d{12}$/", $cc_num);
break;

case "Diner's Club" :
$cc_good = preg_match ("/^30[0-5]\d{11}$|^3[68]\d{12}$/", $cc_num);
break;

case "Japan Credit Bank" :
$cc_good = preg_match ("/^3\d{15}$|^2131\d{11}$|1800\d{15}$/", $cc_num);
break;

default :
break;
}[/tt] ______________________________________________________________________
My God! It's full of stars!
______________________________________________________________________
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top