Pixel Envision Ltd.
Mobile Game Development Studio
  • Home
  • About Us
  • Our Games
    • Hyper-Casual
    • Casual Games
      • Don’t Get Caught
      • Hordes of Enemies
      • noded
      • kubic
      • Whip Swing
      • Witches’ Brew
    • Kids Apps
      • Coloring Book
      • Cars & Trucks Puzzle
      • Train Puzzles for Kids
      • Animal Puzzle
      • Fairy Tale Puzzles for Kids
      • Find the Differences
  • Support
  • Privacy Policy
Select Page ...

PHP Credit Card Validation class using Mod 10 (Luhn) + more!

PHP Credit Card Validation class using Mod 10 (Luhn) + more!

February 17, 2012 PHP, Programming

Here is another PHP code snippet from my library. This PHP class allows you to validate credit card information before processing it further. Doing all possible off-line checks before sending it to he credit card gateway will help you save time & money…

If the card is valid it will return cleaned & formatted card number along with the card type (Visa, Mastercard, Amex, Dinners, Discover, JCB & Maestro).

Besides regular checks, validates the card number against Mod 10 (Luhn) as well as know test card numbers…

Features

  • Checks if the card number, expiration date & validation numbers are valid.
  • Checks if the card is expired
  • Finds & returns the card type.
  • Checks against the known test card numbers.
  • Validates the card number using Mod 10 algorithm.

Usage Sample

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
require ('mod10.php');
$CARDTYPE = $ErrArr = NULL;
 
$cc = new CCVal($_POST["card_number"], $_POST["card_expire_month"], $_POST["card_expire_year"], $_POST["card_cvv2"]);
 
$cstatus = $cc->IsValid();
 
if ($cstatus[0] == "valid")
{
    $CARDTYPE = $cstatus[1];
    $_POST["card_number"] = $cstatus[2];
    $_POST["card_cvv2"] = $cstatus[3];
}
else
{
    $ErrArr = $cstatus;
}
?>

Download

  • Download PHP code snippet *UPDATED FOR AMEX CVV FIX
  • Code is PHP5 compatible but it should also work on PHP4 (Untested)

License

This code is free to use, distribute, modify and study. When referencing please link back to this website / post in any way e.g. direct link, credits etc. If you find this useful, please leave a comment and share using the buttons below!

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php class CCVal
{
    var $__ccType = '';
    var $__ccNum = '';
    var $__ccExpM = 0;
    var $__ccExpY = 0;
    var $__ccCVV = '';
    function CCVal($num, $expm, $expy, $cvv)
    {
        if (!empty($num))
        {
            $cardNumber = ereg_replace("[^0-9]", "", $num);
            if (!empty($cardNumber))
            {
                $this - &gt;
                __ccNum = $cardNumber;
            }
        }
 
        if (!empty($cvv))
        {
            $cardCVV = ereg_replace("[^0-9]", "", $cvv);
            if (!empty($cardCVV))
            {
                $this - &gt;
                __ccCVV = $cardCVV;
            }
        }
 
        if (!is_numeric($expm) || $expm & lt;
        1 || $expm & gt;
        12)
        {
            $this - &gt;
            __ccExpM = NULL;
        }
        else
        {
            $this - &gt;
            __ccExpM = $expm;
        }
 
        $currentYear = date('Y');
        settype($currentYear, 'integer');
        $expy = $expy + 2000;
        if (!is_numeric($expy) || $expy & lt;
        $currentYear || $expy & gt;
        $currentYear + 15)
        {
            $this - &gt;
            __ccExpY = NULL;
        }
        else
        {
            $this - &gt;
            __ccExpY = $expy;
        }
 
        if (ereg("^5[1-5][0-9]{14}$", $this - &gt;
        __ccNum))
        {
            $this - &gt;
            __ccType = MASTERCARD;
        }
        elseif (ereg("^4[0-9]{12}([0-9]{3})?$", $this - &gt;
        __ccNum))
        {
            $this - &gt;
            __ccType = VISA;
        }
        elseif (ereg("^3[47][0-9]{13}$", $this - &gt;
        __ccNum))
        {
            $this - &gt;
            __ccType = AMEX;
        }
        elseif (ereg("^3(0[0-5]|[68][0-9])[0-9]{11}$", $this - &gt;
        __ccNum))
        {
            $this - &gt;
            __ccType = DINNERS;
        }
        elseif (ereg("^6011[0-9]{12}$", $this - &gt;
        __ccNum))
        {
            $this - &gt;
            __ccType = DISCOVER;
        }
        elseif (ereg("^(3[0-9]{4}|2131|1800)[0-9]{11}$", $this - &gt;
        __ccNum))
        {
            $this - &gt;
            __ccType = JCB;
        }
        elseif (ereg("^(5[06-8]|6)[0-9]{10,17}$", $this - &gt;
        __ccNum))
        {
            $this - &gt;
            __ccType = MAESTRO;
        }
        else
        {
            $this - &gt;
            __ccType = UNKNOWN;
        }
    }
 
    function IsValid()
    {
 
        $validFormat = false;
        $validExp = false;
        $passCheck = false;
        $validCVV = false;
        $testCard = false;
 
        switch ($this - &gt;
        __ccType)
        {
            case MASTERCARD:
                $validFormat = true;
            break;
            case VISA:
                $validFormat = true;
            break;
            case AMEX:
                $validFormat = true;
            break;
            case DISCOVER:
                $validFormat = true;
            break;
            case DINNERS:
                $validFormat = true;
            break;
            case JCB:
                $validFormat = true;
            break;
            case MAESTRO:
                $validFormat = true;
            break;
            default:
 
                $validFormat = false;
        }
 
        $cardExpM = $this - &gt;
        __ccExpM;
        $cardExpY = $this - &gt;
        __ccExpY;
 
        if ($cardExpM & amp; & amp;
        $cardExpY & amp; & amp;
        date('Ym') & lt; = $cardExpY . $cardExpM)
        {
            $validExp = true;
        }
        $cardCVV = $this - &gt;
        __ccCVV;
 
        if ($cardCVV & amp; & amp;
        (strlen($cardCVV) == 3 & amp; & amp;
        $this - &gt;
        __ccType != AMEX))
        {
            $validCVV = true;
        }
        elseif ($cardCVV & amp; & amp;
        (strlen($cardCVV) == 4 & amp; & amp;
        $this - &gt;
        __ccType == AMEX))
        {
            $validCVV = true;
        }
 
        $cardNumber = strrev($this - &gt;
        __ccNum);
        $numSum = 0;
 
        for ($i = 0;$i & lt;strlen($cardNumber);$i++)
        {
            $currentNum = substr($cardNumber, $i, 1);
            if ($i % 2 == 1)
            {
                $currentNum *= 2;
            }
            if ($currentNum & gt;
            9)
            {
                $firstNum = $currentNum % 10;
                $secondNum = ($currentNum - $firstNum) / 10;
                $currentNum = $firstNum + $secondNum;
            }
 
            $numSum += $currentNum;
        }
 
        $passCheck = ($numSum % 10 == 0);
 
        $testCard = in_array($this - &gt;
        __ccNum, array(
            '340000000000009',
            '341111111111111',
            '343434343434343',
            '346827630435344',
            '370000000000002',
            '370000200000000',
            '370407269909809',
            '370556019309221',
            '371449635398431',
            '374200000000004',
            '376462280921451',
            '377752749896404',
            '378282246310005',
            '378734493671000',
            '30000000000004',
            '30569309025904',
            '5019717010103742',
            '30204169322643',
            '30218047196557',
            '30221511563252',
            '36000000000008',
            '36148900647913',
            '36700102000000',
            '38000000000006',
            '38520000023237',
            '6011000000000004',
            '6011000000000012',
            '6011000400000000',
            '6011000990139424',
            '6011111111111117',
            '6011153216371980',
            '6011601160116611',
            '6011687482564166',
            '6011814836905651',
            '201400000000009',
            '201481123699422',
            '214925980592653',
            '214983972181233',
            '180001638277392',
            '180040153546898',
            '180058601526635',
            '3528000700000000',
            '3528723740022896',
            '3530111333300000',
            '3566002020360505',
            '3569990000000009',
            '630495060000000000',
            '6304900017740292441',
            '6333333333333333336',
            '5100080000000000',
            '5105105105105100',
            '5111111111111118',
            '5123619745395853',
            '5138495125550554',
            '5274576394259961',
            '5301745529138831',
            '5311531286000465',
            '5364587011785834',
            '5404000000000001',
            '5424000000000015',
            '5431111111111111',
            '5454545454545454',
            '5459886265631843',
            '5460506048039935',
            '5500000000000004',
            '5500939178004613',
            '5555555555554444',
            '5565552064481449',
            '5597507644910558',
            '6334580500000000',
            '6334900000000005',
            '633473060000000000',
            '6767622222222222222',
            '6767676767676767671',
            '5641820000000005',
            '6331101999990016',
            '6759649826438453',
            '4007000000027',
            '4012888818888',
            '4024007127653',
            '4222222222222',
            '4556069275201',
            '4556381812806',
            '4911830000000',
            '4916183935082',
            '4916603452528',
            '4929000000006',
            '4005550000000019',
            '4012888888881881',
            '4111111111111111',
            '4444333322221111',
            '4539105011539664',
            '4544182174537267',
            '4716914706534228',
            '4916541713757159',
            '4916615639346972',
            '4917610000000000',
            '4406080400000000',
            '4462000000000003',
            '4462030000000000',
            '4917300000000008',
            '4917300800000000',
            '4484070000000000',
            '4485680502719433'
        ));
 
        if ($validFormat & amp; & amp;
        $validExp & amp; & amp;
        $validCVV & amp; & amp;
        $passCheck & amp; & amp;
        !$testCard)
        {
            return array(
                "valid",
                $this - &gt;
                __ccType,
                $this - &gt;
                __ccNum,
                $this - &gt;
                __ccCVV
            );
        }
        else
        {
            $ERR = NULL;
            if (!$validFormat || !$passCheck) $ERR[] = "Invalid card number";
            if (!$validCVV) $ERR[] = "Invalid CVV2/CVC2 Number";
            if (!$validExp) $ERR[] = "Credit card has been expired";
            if ($testCard) $ERR[] = "Reserved card number";
            return $ERR;
        }
    }
}
?>

← We have stopped developing with the Corona SDK
Corona Project Manager license for sale! →

5 Responses to PHP Credit Card Validation class using Mod 10 (Luhn) + more!

  • Aina
    11 / 29 / 2015

    hye, could you help me with these error?
    if (preg_match(‘/^5[1-5][0-9]{14}$/’, $this->__ccNum)) {
    but THIS line, and the next 3 elseif’s do not pass:
    elseif (preg_match(“/^3[47][0-9]{13}$/”, $this->__ccNum)) {

    Aina 11 / 29 / 2015
    • Erdener Gonenc
      12 / 2 / 2015

      Hard to tell but from the sample it might be syntax error. I would check quotation marks & closing brackets. I hope this helps.

      Erdener Gonenc 12 / 2 / 2015
  • ervasti
    12 / 29 / 2013

    Hi! never mind – I fixed it. Thanks a bunch!~ =]

    ervasti 12 / 29 / 2013
    • Erdener
      12 / 30 / 2013

      Ah, ok. You’re welcome… :)

      Erdener 12 / 30 / 2013
  • ervasti
    12 / 29 / 2013

    Hi! ereg is throwing deprecation errors. I’ve tried for 3 hours to edit your code using preg_match and it is say “unexpected ‘^’ on L74.” Here is an example:

    THIS line seems to pass the error test:
    if (preg_match(‘/^5[1-5][0-9]{14}$/’, $this->__ccNum)) {
    but THIS line, and the next 3 elseif’s do not pass:
    elseif (preg_match(“/^3[47][0-9]{13}$/”, $this->__ccNum)) {

    Any thoughts? Thanks!

    ervasti 12 / 29 / 2013
  • Tags

    3ds Max Coming Soon CoronaSDK Featured Flash Lua MAXScript PHP Programming Reviews Tips & Tricks Unity 3D Windows Phone
  • Recent Comments

    • Yogesh Singh on ZIP (POSTAL) Code Validation Regex & PHP code for 12 Countries
    • Admin on Maxscript – Vray Cubemap Generator for Unity
    • charlie on Maxscript – Vray Cubemap Generator for Unity
    • Mastan on PHP Currency Converter
    • Rakesh Vishnoi on ZIP (POSTAL) Code Validation Regex & PHP code for 12 Countries
    • Find us on

      amazonandroidapplefacebooklinkedintwitterwindowsyoutube
    • Company Information

      Lytchett House, 13 Freeland Park, Wareham Road, Poole, Dorset, BH16 6FA

      Pixel Envision Limited is a company registered in England, company number: 09558675. Registered Office: Preston Park House, South Road, Brighton, East Sussex, BN1 6SB, United Kingdom

    • Privacy Policy
    Copyright © 2011-2021 Pixel Envision Ltd, all rights reserved.