We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

reCAPTCHA integration

Hello,

I have to put reCAPTCHA on my account registration. Is there any updateted packet, or best way to do it :) I have angular form on front end so basicly i need just to validate it in phalcon.

Tips are welcome, Thank you



85.5k

composer

"google/recaptcha" : "~1.1",

bootstrap.php


$this->di->setShared("recapcha", function () use ($config) {

    return new \ReCaptcha\ReCaptcha($config->reCapchaSercret);
});

function that i call at the begining of an controller action

public function validateCapcha(){

        if (APPLICATION_ENV === "development"){
            return true;
        }

        if ($_POST["g-recaptcha-response"]=== ""){
            throw new whatever
        }

        $ip = $this->request->getClientAddress();

        $check = $this->recapcha->verify($data['g-recaptcha-response'], $ip);

        if (!$check->isSuccess()){
            throw new whatever
        }
    }

Wtf $_POST ? XD



85.5k

specially for you, the original code


public function validateCapcha() {

        if (APPLICATION_ENV === "development") {
            return true;
        }

        $data = (new \Shop\Utils\Caster($_POST, [
            "g-recaptcha-response" => "string",
        ]))->getData();

        if ($data['g-recaptcha-response'] === "") {
            throw new \Shop\Exceptions\Capcha();
        }

        $ip = $this->request->getClientAddress();

        $check = $this->recapcha->verify($data['g-recaptcha-response'], $ip);

        if (!$check->isSuccess()) {
            throw new \Shop\Exceptions\Capcha();
        }
    }

https://github.com/phalcon/forum/blob/master/app/provider/Captcha/Adapters/ReCaptcha.php https://github.com/phalcon/forum/blob/master/app/provider/Captcha/ServiceProvider.php

        if ($this->request->isPost()) {
            if (!$this->checkTokenPost('create-post') || !$this->checkCaptcha()) {
                $this->response->redirect();
                return;
            }

       ...

try to use $this->response->getPost