PHP code example of phpdevsr / recaptcha-codeigniter4
1. Go to this page and download the library: Download phpdevsr/recaptcha-codeigniter4 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/ */
phpdevsr / recaptcha-codeigniter4 example snippets
onfig\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);
}
}
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);
}
}
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);
}
}