PHP code example of captchaforce / recaptcha

1. Go to this page and download the library: Download captchaforce/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/ */

    

captchaforce / recaptcha example snippets


use Recaptcha\Recaptcha;
use Recaptcha\CaptchaType;

Recaptcha::setCaptcha(5);
Recaptcha::captchaImage(Recaptcha::getCaptcha(CaptchaType::TEXT));

use Recaptcha\Recaptcha;
use Recaptcha\CaptchaType;

Recaptcha::setCaptcha(5);
Recaptcha::captchaImage(Recaptcha::getCaptcha(CaptchaType::NUMBER));

Recaptcha::setCaptcha(8);

use Recaptcha\Recaptcha;

$result = Recaptcha::verifyCaptcha($text_inputan_user);
echo $result ? "Captcha valid" : "Captcha tidak valid !";


defined('BASEPATH') or exit('No direct script access allowed');

use Recaptcha\Recaptcha;

class Login extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        Recaptcha::setCaptcha(5); // Bagian terpenting
    }
    
    public function index()
    {
        $this->load->view('dashboard/login');
    }
}


defined('BASEPATH') or exit('No direct script access allowed');

use Recaptcha\Recaptcha;
use Recaptcha\CaptchaType;

class Captchaimg extends CI_Controller
{   
    public function index()
    {
        Recaptcha::captchaImage(Recaptcha::getCaptcha(CaptchaType::NUMBER)); // Bagian terpenting
    }
}

public function login()
{
    if ($this->input->post()) {
        $this->form_validation->set_rules('username', 'Username', 'ptcha', 'Captcha', 'put->post('password');
            $captcha = $this->input->post('captcha');

            if(Recaptcha::verifyCaptcha($captcha)) { // Bagian terpenting
                $user = $this->user->login($username, $password);

                if ($user) {
                $this->session->set_userdata('user', $user);
                
                redirect('dashboard');
                } else {
                    $data['error'] = 'Username atau password salah.';
                }
            }else{
                $data['error'] = 'Captcha salah';
            }
        } else {
            $data['error'] = validation_errors();
        }
    }

    $this->load->view('login');
}
html
<form action=" echo site_url('auth/login'); 
bash
php ./vendor/bin/phpunit --testdox tests