LUHN Algorithm

Recently I was chatting with a friend on creditcard validation. He was looking for ways to validate a creditcard via javascript. While it’s certainly possible, and there’s alot of sample code out there for it, I just cant bring myself to use any kind of client-side process for verification of what in my opinion is crucial information such as creditcard numbers.

For one, you can disable javascript, which may bypass validation entirely (depending on how the form submit is being handled). For another, you could, with the right Firefox plugins, just edit the javascript function directly in the page and have it return “true” to get a successful submission. So I was giving him some sample sites utilizing PHP LUHN algorithms, and noticed something interesting – As with the javascript versions, most of them don’t work or have flaws that haven’t been fixed. One must wonder how many people out there are copy/pasting these buggy versions for use in professional websites.

I decided to dig up my own LUHN algorithm, and did some tweaks to it before posting it here. It’s 100% operational, and so far I’ve been unable to produce any false results with it.

    function IsValidCC($cc) {
	$cc=str_replace("-","",$cc);
        for($i = strlen($cc); $i >=0; --$i) {
            $dnum .= ($odd = !$odd) ? $cc[$i]*2 : $cc[$i];
        }
        for($i = 0; $i < strlen($dnum); $i++) {
            $sum += (int)$dnum[$i];
        }
        return (($sum % 10 ==0) && ($sum != 0));
    }

Without alot of dramatic analysis of its inner workings and so on, here are the numbers I tested with, their results, and the processing time for the entire bunch.

Validating 55 numbers

true [5328304431046754]
true [5103312845585953]
true [5310941494599177]
true [5502457933980793]
true [5526386293832793]
true [5541030109388076]
true [5160933962299217]
true [5516418411392004]
true [5202598727749606]
true [5319527039331431]
true [4539642231959702]
true [4916962068349733]
true [4556574648962060]
true [4556389698328918]
false [4916971931396181]
false [4485099272362583]
true [4929771078260656]
true [4532940023485978]
true [4156763252772170]
true [4916726708254888]
true [4929857283556]
true [4539778019847]
true [4024007176858]
true [4556449022851]
true [4716337992272]
true [340315629019234]
true [371245491835627]
true [348410551014014]
false [374973107447824]
true [343837789734937]
true [6011337931905012]
true [6011750581974742]
true [6011625028562351]
true [36673333924894]
true [30326629585715]
true [30036607370614]
true [201463953817936]
false [201466327996032]
true [201473001172531]
true [180090307342009]
true [180045216543949]
true [210005131596400]
true [3088574904967422]
true [3528446407813882]
true [3158631681355554]
true [869940964429408]
true [869953540433067]
true [869965600155299]
false [10]
false [55]
false [0]
false [-1]
false [-5-5]
false [abcde]
false []

Processing time: 0.00612s

Not bad, if you ask me. :)

Popularity: 98% [?]

programming - PHP

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

4 Responses to “LUHN Algorithm”

Leave Comment

(required)

(required)