Credit Card Validation Script Class Shows Type Of Credit Card
credit card validation script class shows type of credit card
credit, card, validation, script, class, shows, type, of, credit, card
Credit Card Validation Script Class Shows Type Of Credit Card
Post Description: credit card validation script class shows type of credit card
POST# 672
Posted On: Fri Feb 29, 2008 12:01 pm
juilos
Topic: Credit Card Validation Script Class Shows Type Of Credit Card
hi, im new to php and i was wondering if you can show me how i can have a form and then check the credit card number to determine what type of credit cart it is. for example, if a user enters a credit card, i want to know if its a visa or master card that way i can send it to my credit card like paypal pro.

thanks
Share:
BBCODE:
HTML Code:


Wed Jan 30, 2008 9:50 am
1
creditcards
Reply #1553
i use a good script for this,

just save this as card-type.php and upload it to your server and test it with your browser:

card-type.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHP Credid Card Validation Script Form</title>
<meta name="description" content="a php script showing a tutorial how to create a script about credit card validation with a form">
<meta name="keywords" content="php, credit, card, validator, validation, script, class">
</head>
<body>
<?php
 
function validateCC($ccnum){
// Clean up input
$ccnum = ereg_replace('[-[:space:]]', '',$ccnum);

// What kind of card do we have
$type = check_type($ccnum);
// Does the number matchup ?
$valid = check_number($ccnum);
return array($type, $valid);
}

// Prefix and Length checks
function check_type( $cardnumber ) {
$cardtype = "UNKNOWN";
$len = strlen($cardnumber);
if ( $len == 15 && substr($cardnumber, 0, 1) == '3' ) { $cardtype = "amex"; }
elseif ( $len == 16 && substr($cardnumber, 0, 4) == '6011' ) { $cardtype = "discover"; }
elseif ( $len == 16 && substr($cardnumber, 0, 1) == '5' ) { $cardtype = "mc"; }
elseif ( ($len == 16 || $len == 13) && substr($cardnumber, 0, 1) == '4' ) { $cardtype = "visa"; }
return ( $cardtype );
}

// MOD 10 checks
function check_number( $cardnumber ) {
$dig = toCharArray($cardnumber);
$numdig = sizeof ($dig);
$j = 0;
for ($i=($numdig-2); $i>=0; $i-=2){
$dbl[$j] = $dig[$i] * 2;
$j++;
}
$dblsz = sizeof($dbl);
$validate =0;
for ($i=0;$i<$dblsz;$i++){
$add = toCharArray($dbl[$i]);
for ($j=0;$j<sizeof($add);$j++){
$validate += $add[$j];
}
$add = '';
}
for ($i=($numdig-1); $i>=0; $i-=2){
$validate += $dig[$i];
}
if (substr($validate, -1, 1) == '0') { return 1; }
else { return 0; }
}

// takes a string and returns an array of characters
function toCharArray($input){
$len = strlen($input);
for ($j=0;$j<$len;$j++){
$char[$j] = substr($input, $j, 1);
}
return ($char);
}

$ccnumber = $_POST['ccnumber'];
list($type, $valid) = validateCC($ccnumber);
if ( $valid ) {
// Do something fun with the card
echo "Number: <b>$ccnumber</b> ... Type: <b>$type</b> ... Valid: <b>$valid</b>";
}
else {
// Return some sort of error
echo "INVALID: Number: <b>$ccnumber</b> ... Type: <b>$type</b> ... Valid: <b>$valid</b>";
}
?>
<h1>PHP Credid Card Validation Script Form </h1>
<hr>
<form action='' method="post">
Card Number: <input name='ccnumber' />
<input type='submit' />
</form>
<table style="font-family: courier; margin-left: 20px; line-height: 0.9em;">
<tr><td style="padding-right: 30px;">American Express</td><td>3400 0000 0000 009</td></tr>
<tr><td>Carte Blanche</td><td>3000 0000 0000 04</td></tr>
<tr><td>Discover</td><td>6011 0000 0000 0004</td></tr>
<tr><td>Diner's Club</td><td>3000 0000 0000 04</td></tr>
<tr><td>enRoute</td><td>2014 0000 0000 009</td></tr>
<tr><td>JCB</td><td>2131 0000 0000 0008</td></tr>
<tr><td>MasterCard</td><td>5500 0000 0000 0004</td></tr>
<tr><td>Solo</td><td>6334 0000 0000 0004</td></tr>
<tr><td>Switch</td><td>4903 0100 0000 0009</td></tr>
<tr><td>Visa</td><td>4111 1111 1111 1111</td></tr>
</table>
<div align="center">
<p>&nbsp;</p>
<p><a href="http://www.webune.com">Tutorial hosing by webune.com</a> </p>
</div>
</body>
</html>



if you want to see it in an example, you can go to this link:

PHP Credid Card Validation Script Form

What do you think?

* name:  

* email:  

* Please enter comments:


Receive Replies on my Comments
(An email will be sent to you when someone replies to your comments)

Add image to comments
yes no             upload