Zf3 google Re-captcha problem

zend-form

#1

hi there, I have a problem using ZendService\ReCaptcha\ReCaptcha in my Contact form. I already registered in https://www.google.com/recaptcha/admin.
I also installed ZendService\ReCaptcha\ReCaptcha from https://github.com/zendframework/ZendService_ReCaptcha and here is my composer.json code:

{
"name": "zendframework/skeleton-application",
"description": "Skeleton Application for Zend Framework zend-mvc applications",
"type": "project",
"license": "BSD-3-Clause",
"keywords": [
    "framework",
    "mvc",
    "zf"
],
"homepage": "http://framework.zend.com/",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
    "php": "^5.6 || ^7.0",
    "zendframework/zend-component-installer": "^1.0 || ^0.7 || ^1.0.0-dev@dev",
    "zendframework/zend-mvc": "^3.0.1",
    "zfcampus/zf-development-mode": "^3.0",
    "zendframework/zend-db": "^2.8.1",
    "zendframework/zend-mvc-form": "^1.0",
    "zendframework/zend-json": "^3.1",
    "zendframework/zend-session": "^2.7.1",
    "zendframework/zend-navigation": "^2.8",
    "zendframework/zend-paginator": "^2.7",
    "zendframework/zend-permissions-acl": "^2.6",
    "tasmaniski/zend-layout-change": "^1.0",
    "zendframework/zend-captcha": "^2.7",
    "zendframework/zendservice-recaptcha": "*",
    "zendframework/zend-http": "^2.5.4"
},
"autoload": {
    "psr-4": {
        "Application\\": "module/Application/src/",
        "Blog\\": "module/Blog/src/",
        "PageBuilder\\": "module/PageBuilder/src",
        "Contact\\": "module/Contact/src",
        "ZendService\\ReCaptcha\\": "src/"
    }
},
"autoload-dev": {
    "psr-4": {
        "ApplicationTest\\": "module/Application/test/"
    }
},
"extra": {
    "branch-alias": {
        "dev-master": "3.0-dev",
        "dev-develop": "3.1-dev"
    }
}
,"repositories": [
    {
        "type": "composer",
        "url": "https://packages.zendframework.com/"
    }
]
,
"scripts": {
    "cs-check": "phpcs",
    "cs-fix": "phpcbf",
    "development-disable": "zf-development-mode disable",
    "development-enable": "zf-development-mode enable",
    "development-status": "zf-development-mode status",
    "post-create-project-cmd": [
        "@development-enable"
    ],
    "serve": "php -S 0.0.0.0:8080 -t public public/index.php",
    "test": "phpunit"
     }
  }

here is my Contact\Controller\IndexController for using re-captcha:

        public function indexAction()
        {
            $recaptcha = new \ZendService\ReCaptcha\ReCaptcha(
            'PUB_KEY',
            'PRIVATE_KEY'); 
             return new ViewModel
            ([
            'recaptcha'=>$recaptcha,
            ]);
        }

and here the index.phtml code:

            echo $this->recaptcha->getHTML();
            if(isset($_POST['recaptcha_response_field'])){
                $recaptcha_challenge =$_POST['recaptcha_challenge_field'];
                $recaptcha_response = $_POST['recaptcha_response_field'];
                if(isset($recaptcha_challenge)&&
                    isset($recaptcha_response) ||
                    !empty($recaptcha_challenge)&&
                    !empty($recaptcha_response)){
                    $result = $this->recaptcha->verify(
                        $recaptcha_challenge,
                        $recaptcha_response
                    );
                    if (!$result->isValid())
                    {
                        echo $result->getErrorCodes();
                    }
                }
            }

but after all my form is submitted without filling re-captcha part. and there is no error. what should I do?


#2

I haven’t used ZendService_ReCaptcha myself so I can’t help you with that. But I do hope those are not your real recaptcha keys, although they don’t look like the official test keys. You have a public and a secret key for a reason!

EDIT: Removed the keys for you. I suggest to create a new set and don’t post those on a public forum.


#3

the work is done by adding ZendService\ReCaptcha . then back to Controller and instantiate the class using $recaptcha = new \ZendService\ReCaptcha\ReCaptcha($pubKey, $privKey); the next step will be pass $recaptcha to specified ViewModel and in the view(in my project index.phtm) render the html recaptcha field using recaptcha->getHTML(). when a user fill out the recaptcha in the controller we receive **_POST[‘g-recaptcha-response’]** . check the validation in Controller:

$result =$recaptcha->verify( $_POST['g-recaptcha-response'])
if(!$result->isValid())
{
 throw new \Exception('recaptcha is not valid');
}

and that’s all the following code is your specific logic to handle form submission!