OpenCart CAPTCHA not working JFIF gd-jpeg v1.0 [SOLVED]

Posted by Paul on November 26, 2014

I came across an issue with OpenCart CAPTCHAs recently for the about the third time, which was difficult to fix each time. Unfortunately for me, the fix I'd applied previously didn't work!

The symptom of this issue is that on pages where you need the user to solve a CAPTCHA before posting such as the contact form, product reviews etc. There is a blank space where the CAPTCHA image should be.

If you look in the HTML code at the img src attribute, you'll see a link very much like this "http://www.yoursite.com/index.php?route=product/product/captcha". If all was working correctly, this should provide image data that the browser can use.

If you browse to that link directly, instead of seeing an image you'll see a scrambled load of image data like this:

ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛC       $.' ",#(7),01444'9=82

What you should see is:

index.jpg

So, what's the cause and how did I fix it this time?

It seems to be caused every time by some whitespace characters (spaces, tabs, carriage returns) being added to the CAPTCHA code, which then confuse the function that outputs the image.

Previously I had added this bit of code to /system/library/captcha.php:

function getCode(){
    $out = ob_get_contents();
    $out = str_replace(array("\n", "\r", "\t", " "), "", $this->code);
    ob_end_clean();
    return $out;
}

But unfortunately this time my old friend (i.e. the snippet above) wasn't able to help. It occurred to me that further down in the file, $this->code is being accessed but not through its "getter", getCode().

Adding this line to the constructor in that same file, however seemed to fix the issue:

$this->code = str_replace(array("\n", "\r", "\t", " "), "", $this->code);

So the constructor would now look like:

function __construct() {
    $this->code = substr(sha1(mt_rand()), 17, 6);
    $this->code = str_replace(array("\n", "\r", "\t", " "), "", $this->code);
}

If you'd rather not modify this system file, this extension should fix it:
http://www.opencart.com/index.php?route=extension/extension/info&extension_id=19809

Did this fix your issue? Need one of our developers to apply it for you? Let us know in the comments!

blog comments powered by Disqus