Skip to content

PHPDevsr/reCaptcha-Codeigniter4

Repository files navigation

PHPUnit PHPStan Coverage Status Downloads GitHub release (latest by date) GitHub stars GitHub license

reCAPTCHA Codeigniter 4

Free to Use Library reCAPTCHA v2 for Codeigniter 4.

What is reCAPTCHA?

reCAPTCHA is a free service that protects your site from spam and abuse. It uses advanced risk analysis engine to tell humans and bots apart. With the new API, a significant number of your valid human users will pass the reCAPTCHA challenge without having to solve a CAPTCHA (See blog for more details). reCAPTCHA comes in the form of a widget that you can easily add to your blog, forum, registration form, etc.

See the details.

Sign up for an API key pair

To use reCAPTCHA, you need to sign up for an API key pair for your site. The key pair consists of a site key and secret. The site key is used to display the widget on your site. The secret authorizes communication between your application backend and the reCAPTCHA server to verify the user's response. The secret needs to be kept safe for security purposes.

Installation

install with composer

$ composer require phpdevsr/recaptcha-codeigniter4
$ php spark config:publish

Usage

Since v2.1.0, environment config can be use for multiple config with .env

recaptcha.recaptchaSiteKey
recaptcha.recaptchaSecretKey
recaptcha.recaptchaLang

Initialization

<?php

require 'vendor/autoload.php';

use Config\Recaptcha as RecaptchaConfig;
use PHPDevsr\Recaptcha\Recaptcha;

class Example
{
    /**
     * Config Recaptcha
     * 
     * @var RecaptchaConfig $config
     */
    protected RecaptchaConfig $config;

    /**
     * Recaptcha
     * 
     * @var Recaptcha $recaptcha
     */
    protected Recaptcha $recaptcha;

    public function __construct()
    {
        // Set Config
        $this->config = new RecaptchaConfig();

        $this->recaptcha = new Recaptcha($this->config);
    }
}

Since v2.0.0, you can using helper

<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index(): string
    {
        helper('recaptcha');

        $data = [
            'scriptTag' => getScriptTag(),
            'widgetTag' => getWidget(),
        ];

        $captcha = $this->request->getPost('g-recaptcha-response');
        $response = verifyResponse($captcha);

        if (isset($response['success']) and $response['success'] === true) {
            echo "You got it!";
        }

        return view('welcome_message', $data);
    }
}

or directly use service

<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index(): string
    {
        $recaptcha = service('recaptcha');

        $data = [
            'scriptTag' => $recaptcha->getScriptTag(),
            'widgetTag' => $recaptcha->getWidget(),
        ];

        $captcha = $this->request->getPost('g-recaptcha-response');
        $response = $recaptcha->verifyResponse($captcha);

        if (isset($response['success']) and $response['success'] === true) {
            echo "You got it!";
        }

        return view('welcome_message', $data);
    }
}

Render reCAPTCHA Widget

  • Default
echo $this->recaptcha->getWidget();

// Output
<div class="g-recaptcha" data-sitekey="xxxxx" data-theme="light" data-type="image" data-size="normal" loading="lazy"></div>
  • Theme
echo $this->recaptcha->getWidget(array('data-theme' => 'dark'));

// Output
<div class="g-recaptcha" data-sitekey="xxxxx" data-theme="dark" data-type="image" data-size="normal" loading="lazy"></div>
  • Type
echo $this->recaptcha->getWidget(array('data-theme' => 'dark', 'data-type' => 'audio'));

// Output
<div class="g-recaptcha" data-sitekey="xxxxx" data-theme="dark" data-type="audio" data-size="normal" loading="lazy"></div>

Render Script Tag

  • Default
echo $this->recaptcha->getScriptTag();

// Output
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=onload&hl=en" defer></script>
  • Render
echo $this->recaptcha->getScriptTag(array('render' => 'explicit'));

// Output
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=explicit&hl=en" defer></script>
  • Language
echo $this->recaptcha->getScriptTag(array('hl' => 'id'));

// Output
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=onload
&hl=id" defer></script>

Verify Response

Calls the reCAPTCHA siteverify API to verify whether the user passes g-recaptcha-response POST parameter.

$captcha = $this->request->getPost('g-recaptcha-response');
$response = $this->recaptcha->verifyResponse($captcha);

if (isset($response['success']) and $response['success'] === true) {
    echo "You got it!";
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contrib

We does accept and encourage contributions from the community in any shape. It doesn't matter whether you can code, write documentation, or help find bugs, all contributions are welcome.

Made with contrib.rocks.