Check if a creditcard is valid without sending information to an outside website. AMEX, VISA and MASTERCARD supported.
By : smckone
This script is object orianted but I included a simple wrapper funtion.
It makes it easy by just entering the card number and returning a boolean true or false;
boolean IsCardValid(int);
boolean IsCardValid(string);
<?php
/**************************************************
Credit Card Validation
Check if a creditcard is valid without
sending information to an outside
website. AMEX, VISA and MASTERCARD supported.
Sam McKone <sam@evilwalrus.com>
v1.0 (Dec 5. 2001)
***************************************************/
class CCValidation{
// static variables
var $INVALID = -1;
var $VISA = 1;
var $MASTERCARD = 2;
var $AMEX = 3;
var $CARD_NAMES = array("Visa","Mastercard","American Express");
// data members
var $CardNumber = "";
function CCValidation($set_num){
$this->CardNumber = $set_num;
}
function setCardNumber($set_num){
$this->CardNumber = $set_num;
}
function CheckCardID(){
$valid = $INVALID;
$digits1 = substr($this->CardNumber,0,1);
$digits2 = substr($this->CardNumber,0,2);
$digits4 = substr($this->CardNumber,0,4);
if($digit1 == 4){
if(strlen($this->CardNumber) == 13 || strlen($this->CardNumber) == 16){
$valid = $VISA;
}
}else if($digit2 >= 51 && $digit2 <= 55){
if(strlen($this->CardNumber) == 16){
$valid = $MASTERCARD;
}
}else if($digit2 == 34 || $digit2 == 37){
if(strlen($this->CardNumber) == 15){
$valid = $AMEX;
}
}
return $valid;
}
function CheckCardNumber(){
$cardTemp = $this->CardNumber;
$checkSum = 0;
for($i = (strlen($cardTemp) -1);$i >= 0; $i -= 2){
$j = 0;
if(i > 0){
$j = substr($cardTemp,$i-1,$i) * 2;
if($j > 9){
$s = $j;
$j = substr($s,0,1) + substr($s,1);
}
$checkSum += substr($s,0,1) + $j;
}else{
$checkSum += substr($cardTemp,0,1);
}
}
return (($checkSum % 10) == 0);
}
function CheckCard(){
if($this->CheckCardID() != -1){
return $this->CheckCardNumber();
}
return false;
}
}
function IsCardValid($card_num){
$card = new CCValidation($card_num);
return $card->CheckCard();
}
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Miscellaneous Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM DB2 9.5 for Linux, UNIX, and Windows. DB2 9 is the result of a five-year development project that transformed traditional (static) database technology into an interactive data server that merges the high performance and ease of use of DB2 with the self-describing benefits of XML. FREE! Go There Now!
|
|
|
|
Manage, govern, and share services across your organization by using WebSphere Service Registry and Repository. Follow the hands-on exercises to learn how to navigate the Web interface to publish, find, reuse, and update services. FREE! Go There Now!
|
|
|
|
As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br /> FREE! Go There Now!
|
|
|
|
Rational Build Forge Express Edition is an automation framework that packages the latest enterprise-grade technologies into a reliable, flexible and robust configuration designed and priced specifically for small to midsize businesses. The new Rational Build Forge Express eKit provides you with valuable resources – including a case study, podcast, demo, and articles – to help you increase staff productivity, compress development cycles and deliver better software, fast. FREE! Go There Now!
|
|
|
|
As organizations have grown increasingly dependent on online software, the risk of malicious attacks has also become far more serious. Fortunately, well-governed organizations can protect their Web applications by injecting vulnerability assessments and ethical hacks into their software development and delivery processes. This paper describes 12 of the most common hacker attacks and provides basic rules that you can follow to help create more hack-resistant Web applications. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for process. The SOA Sandbox for process focuses on providing a trial environment with the necessary tooling and components required to gain a better understanding of business processes and how to best improve existing business processes to derive value quickly. FREE! Go There Now!
|
|
|
|
The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now. FREE! Go There Now!
|
|
|
|
Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications. FREE! Go There Now!
|
|
|
|
The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |