PHP code example of oppara / cakephp-simple-recaptcha

1. Go to this page and download the library: Download oppara/cakephp-simple-recaptcha library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

oppara / cakephp-simple-recaptcha example snippets



declare(strict_types=1);

namespace App\Controller;

use Cake\Http\Client\Exception\NetworkException;
use Cake\Http\Client\Exception\RequestException;
use Oppara\SimpleRecaptcha\Exception\RecaptchaV3Exception;

class InquiryController extends AppController

    public function initialize(): void
    {
        parent::initialize();

        $this->loadComponent('Oppara/SimpleRecaptcha.Recaptcha', [
            'actions' => [
                'input',
            ],
        ]);
    }

    public function input()
    {
        if ($this->request->is('post')) {

            try {
                if ($this->Recaptcha->verify()) {
                    return $this->redirect(['action' => 'complete']);
                }

                $this->log(json_encode($this->Recaptcha->getResult()), LOG_ERR);
                $this->Flash->error('recaptcha error.');

            } catch (RecaptchaV3Exception $e) {

                $this->log($e->getMessage(), LOG_ERR);
                $this->Flash->error('You have been identified as a robot. Please try again.');

                return $this->redirect(['action' => 'input']);

            } catch (NetworkException | RequestException $e) {
                $this->log($e->getMessage(), LOG_ERR);
                $this->Flash->error('network error.');
            }
        }
    }

    public function complete()
    {
    }
}


declare(strict_types=1);

namespace App\Controller;

use Cake\Http\Client\Exception\NetworkException;
use Cake\Http\Client\Exception\RequestException;

class InquiryController extends AppController

    public function initialize(): void
    {
        parent::initialize();

        $this->loadComponent('Oppara/SimpleRecaptcha.Recaptcha');
    }

    public function index()
    {
        if ($this->request->is('post')) {

            try {
                if ($this->Recaptcha->verify()) {
                    return $this->redirect(['action' => 'complete']);
                }

                $this->log(json_encode($this->Recaptcha->getResult()), LOG_ERR);
                $this->Flash->error('recaptcha error.');

            } catch (NetworkException | RequestException $e) {
                $this->log($e->getMessage(), LOG_ERR);
                $this->Flash->error('network error.');
            }
        }
    }

    public function complete()
    {
    }
}


declare(strict_types=1);

namespace App\Controller;

use Cake\Http\Client\Exception\NetworkException;
use Cake\Http\Client\Exception\RequestException;
use Oppara\SimpleRecaptcha\Exception\RecaptchaV3Exception;

class InquiryController extends AppController

    public function initialize(): void
    {
        parent::initialize();

        $this->loadComponent('Oppara/SimpleRecaptcha.Recaptcha', [
            'actions' => [
                'input',
            ],
        ]);
    }

    public function input()
    {
        if ($this->request->is('post')) {

            try {
                if ($this->Recaptcha->verify()) {
                    return $this->redirect(['action' => 'complete']);
                }

                $this->log(json_encode($this->Recaptcha->getResult()), LOG_ERR);
                $this->Flash->error('recaptcha error.');

            } catch (NetworkException | RequestException $e) {
                $this->log($e->getMessage(), LOG_ERR);
                $this->Flash->error('network error.');
            }
        }
    }

    public function complete()
    {
    }
}


<?= $this->fetch('scriptBottom'); 

<?= $this->Form->create() 


<?= $this->fetch('scriptBottom'); 

<?= $this->Form->create() 


<?= $this->fetch('scriptBottom'); 

<?= $this->Form->create()