We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43592
    • 97 Posts
    Hi forum members,

    Because of sepiarivers recaptcha2 beta-plugin doesnt work at all for me on Formit etc. I tried to integrate the google php-response code as a formit hook:
    Problem: Recaptcha2 shows up nicely but form is submitting by leaving the recaptcha unchecked,too ..
    Any ideas?

    Formit call:
    [[!FormIt?
       &hooks=`spam,email,!myrecaptcha2,redirect`
       &emailTpl=`tpl_kontakt`
       &emailTo=`[[!getmaildata]]`
    &emailBCC=`[[+fi.email]]`
    &emailSubject=`message for ..`
       &redirectTo=`41`
       &validate=`nachname:required,
          email:email:required,
          nachricht:required:stripTags`
    
    ]]
    


    myrecaptcha2-Snippet
    <?php
    $recaptcha = new \ReCaptcha\ReCaptcha(0239432040); //secretcode-numbers
    $resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
    if ($resp->isSuccess()) {
        // verified!
    } else {
        $errors = $resp->getErrorCodes();
    	return false;
    }
    


    Thanks in advance!

    html
    <div class="g-recaptcha" data-sitekey="666publickey"></div>
    


      • 3749
      • 24,544 Posts
      Is the form redirecting, or just reloading?

      By having the email hook ahead of the ReCaptcha hook, the email will be sent before ReCaptcha does its check.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 43592
        • 97 Posts
        Thank you, I changed that, it was redirecting, also because I put a "!" in front of the snippet,too.
        So the snippet wasnt recogniced how i see in the protocols. My aim was to uncache it...

        Now i did

        [[!FormIt?
           &hooks=`spam,myrecaptcha2,email,redirect`


        and get a server error without any messages now out of the protocols by sending the form.

        Dont know why, the php is original from google...
        For this my snippet knowledge still seems to be weak.
          • 3749
          • 24,544 Posts
          Try adding this at the very end of the snippet:

          return true;
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 43592
            • 97 Posts
            Quote from: BobRay at Apr 08, 2016, 02:28 AM
            Try adding this at the very end of the snippet:

            return true;

            The snippet still leads to server error ..
              • 3749
              • 24,544 Posts
              That code requires an autoloader or an include statement. Do you have one or the other?

              Also, I'd recommend adding some sanity checking for the include (if any) and the instantation:

              <?php
              $recaptcha = new \ReCaptcha\ReCaptcha(0239432040); //secretcode-numbers
              
              if (empty($recaptcha) || (! $recaptcha instanceof \ReCaptcha\Recaptcha)) {
                 $modx->log(modX::LOG_LEVEL_ERROR, '[recaptcha2] Instantiation failed');
                 return '';
              }
              
              $resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
              if ($resp->isSuccess()) {
                  // verified!
              } else {
                  $errors = $resp->getErrorCodes();
                  return false;
              }
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 43592
                • 97 Posts
                I'm not sure what you exactly mean.

                What I did ist including the code above and "return true" at the end of it in a internal snippet.
                This I try to call via

                [[!FormIt?
                   &hooks=`spam,myrecaptcha2,email,redirect`


                I get no "[recaptcha2] Instantiation failed" error in the error log but after submitting the form I get a server error
                  • 3749
                  • 24,544 Posts
                  You haven't really answered my question about whether you have an 'include' or 'require' statement to include the file containing the Recaptca class code, or an include statement to include an autoloader. You need one or the other for the code to work (unless the class is defined in the snippet code). Otherwise the first line of the code above will return a PHP error "Class not found" because PHP is not aware of the Recaptcha class. In some circumstances, PHP errors will cause a server error.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 43592
                    • 97 Posts
                    The only thing what I have in addition to the code above is

                    <script src='https://www.google.com/recaptcha/api.js'></script>
                    before the <head> closing tag included in the template as it is said on Google.

                    Is there something wrong or missing? I have no "autoloader" or something else ..
                      • 3749
                      • 24,544 Posts
                      When PHP gets to this line:

                      $recaptcha = new \ReCaptcha\ReCaptcha(0239432040); //secretcode-numbers


                      It checks to see if it has a copy of that class anywhere. If the class definition is not in the current file with the rest of your code, PHP won't be able to find the class code unless you load the file containing the class code yourself with 'require' or 'include' or you load an autoloader with 'require' or 'include' that knows how to find the file with the class code.

                      Without one of those three conditions (class code in current file, class code included, or autoloader file included) Your code will never execute.

                      The simplest solution is to find the file containing the ReCaptcha class and include it above the 'new' line:

                      include 'path/to/file';
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting