2

After a lot of struggle, I've figured out how to implement the standard Joomla reCaptcha.

But now my problem is the verifying of it on the server side.

The steps of the reCaptcha:

  1. I have a form with the recaptcha implemented like this (works fine):

    $captcha_plugin = JFactory::getConfig()->get('captcha');
    if ($captcha_plugin != '0') {
        $captcha = JCaptcha::getInstance($captcha_plugin);
        $field_id = 'captcha';
        print $captcha->display($field_id, $field_id, 'g-recaptcha');
    }
    
  2. On submit I send an AJAX call to this url (works fine):

    'index.php?option=com_content&format=ajax&view=article&task=loginUser&tmpl=component' It's a file I've made inside com_content > views > article

  3. The code in my loginUser task / function (not validating, see end of question)

    public function _logInUser( $form ){
        $captcha_plugin = JFactory::getConfig()->get('captcha');
        $captcha = JCaptcha::getInstance($captcha_plugin);
        $completed = $captcha->CheckAnswer($form[2]['value']);
        var_dump($form);
        echo $form[2]['value'];
        var_dump($completed);
        die();
    

Update: I've also tried like this, still gives me false when it should return true:

JPluginHelper::importPlugin('captcha');
$dispatcher = JEventDispatcher::getInstance();
$res = $dispatcher->trigger('onCheckAnswer',$form[2]['value']);
if(!$res[0]){
    var_dump($res);
    die('Invalid Captcha');
}
var_dump($res);
die();

echo $form[2]['value']; returns "" when captcha is not clicked and returns something like this when clicked: "03AMGVjXgaSV3U5UnezVUIFslFElvrMDe2gOVoJtEwtT9SVMaqG47cyC16CPrl01H3yKVF1y4aaBOnK-E1AeBa1H8XoBE17fN-8L3j0_W5mvO8g9kbrVg-yYEuk1B9lSTWxYfn9kf23yPTY-wlIOijh04vqBwUTLFoiTL-Jw9Ae-l2uvgVrnb1qyBrYt5dtvfRWvwbAx6h6qGXh4WBZz19eFkTv5mTmheGsp4s-5e-VtiqMEQJynNxE0fjxqQxKNyCECAK6SRbtLdZ8NTlDAG4dOd4zfb-R2aIYxwvPOxLjxrlO_5xggTOwqA"

So it must be working.

but this line always returns false : $completed = $captcha->CheckAnswer($form[2]['value']);

and when removing $form[2]['value'] in $captcha->CheckAnswer

I get this output:

Warning: Missing argument 1 for Joomla\CMS\Captcha\Captcha::checkAnswer(), called in /customers/a/c/e/test-mysite.dk/httpd.www/test/components/com_content/views/article/view.ajax.php on line 35 and defined in /customers/a/c/e/test-mysite.dk/httpd.www/test/libraries/src/Captcha/Captcha.php on line 192 Notice: Undefined variable: code in /customers/a/c/e/test-mysite.dk/httpd.www/test/libraries/src/Captcha/Captcha.php on line 200

INFO UPDATE for future readers!: Joomla does NOT provide reCaptcha for the login form. Only for the registration and contact form. I've found it here: https://forum.joomla.org/viewtopic.php?t=910764

  • Any warnings or errors generated from JFactory::getConfig()->get('captcha') or JCaptcha::getInstance($captcha_plugin) or $captcha->CheckAnswer($form[2]['value'])? Have you got debugging turned all the way up? – mickmackusa Oct 18 '18 at 8:13
  • Yes it's set to developer mode. I´ve added all the errors to my question – JonasB Oct 18 '18 at 8:21
  • No, I mean with your originally posted attempt -- any warnings/error with that? – mickmackusa Oct 18 '18 at 8:23
  • ahh!... no that gives me no errors, only the false output of CheckAnswer – JonasB Oct 18 '18 at 8:25
  • 1
    Ah! actually it dosen't show up in the login page when I remove my override of the login page. No warnings though. I think I'll try to install a fresh Joomla on a subdomain and see if I can get it to work just as the normal reCaptcha :(... The only thing I can think off is that I have J2 store installed and that have a plugin for captcha(but the plugin is disabled, so should be that) – JonasB Oct 18 '18 at 10:15
2

Fist of all, PHP is case sensitive. This means that CheckAnswer and checkAnswer is not the same (note the capital c).

The correct usage is $captcha->checkAnswer('your_captcha_id').
What's the problem with your approach:
You are passing the captcha's post result as the ID of your captcha.

Since you have used captcha as the $field_id when you included the captcha, you should write $captcha->checkAnswer('captcha') to check the answer.

  • hmmmm... Still gives me false but this really seem to be the right solution... The strange thing is that I can´t get the reCaptcha to work just as standard inplementation. I'll try to install a fresh Joomla now and see if I can get it to work... Thanks for your answer! I'll let you know what happens – JonasB Oct 19 '18 at 12:23
  • Just found out that Joomla does not provide the captcha for the login form out of the box. I found it here: forum.joomla.org/viewtopic.php?t=910764 ....I will try to copy my files over to a new installation and see if your answer works! :) – JonasB Oct 19 '18 at 15:09
  • I did a lot of testing and trying! I simply can't get it to work :( I managed to get the default Joomla captcha to work on registration and contact forms - so it is installed correctly. But testing my own implementation with your corrections, still gives me a bool(false) response – JonasB Oct 19 '18 at 19:36
  • I've accepted this answer as I found out that if I moved the script to a custom plugin rater than inside com_content > views > article. Then your answer did work! – JonasB Oct 23 '18 at 19:46
  • I'm glad it worked out after all @JonasB – mavrosxristoforos Oct 24 '18 at 13:12
1

Never got it to work with Joomla's $captcha->checkAnswer().

So instead, I've omitted Joomla's checking solution and went straight with Google's checking API.

Here's the code I'm successfully using for the checking part:

$secretKey = "my_secret_key";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$form[2]['value']."&remoteip=".$ip);
var_dump($response);

$form[2]['value'] is the response from the reCaptcha

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.