2017 © Pedro Peláez
 

library codeigniter-recaptcha

CodeIgniter library to use Google's reCAPTCHA V2

image

mehdibo/codeigniter-recaptcha

CodeIgniter library to use Google's reCAPTCHA V2

  • Friday, May 18, 2018
  • by mehdibounya
  • Repository
  • 3 Watchers
  • 10 Stars
  • 24 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 7 Forks
  • 0 Open issues
  • 4 Versions
  • 380 % Grown

The README.md

Codeigniter-recaptcha

This library makes it easy to use Google's reCAPTCHA V2, (*1)

Contents

Installation

Via composer

If you have composer installed you can run, (*2)

composer require mehdibo/codeigniter-recaptcha

Copy the content of config/recaptcha.php to application/config/recaptcha.php, (*3)

First time using composer, (*4)

Open a terminal (commands in windows), the commands here are for linux but you can find the equivalent in windows., (*5)

  1. First thing you should do is install composer, (*6)

  2. Go to your application folder: cd application, (*7)

  3. Install the library: composer require mehdibo/codeigniter-recaptcha, (*8)

  4. Copy the content of config/recaptcha.php to your application/config/recaptcha.php, (*9)

  5. Go to application/config/config.php and set composer_autoload to TRUE, (*10)

  6. That's it! check the Documentation for more details., (*11)

Manually

  1. Download the latest release., (*12)

  2. Copy libraries/Recaptcha.php to application/libraries and config/recaptcha.php to application/config., (*13)

  3. Load the library using the Codeigniter loader $this->load->library('recaptcha'), check the example., (*14)

  4. See the documentation for usage., (*15)

Documentation

Getting the keys

To use the reCAPTCHA you need a pair of keys (A secret and site keys), these can be obtained from Google by going to: https://www.google.com/recaptcha/admin, (*16)

And registering a new website, make sure you tick the "reCAPTCHA V2" option., (*17)

Setting the keys

There are three ways to pass the keys to the library, (*18)

In the config file, (*19)

You can set the keys by editing the config/recaptcha.php config file, (*20)

Using the CodeIgniter loader, (*21)

By passing an array of configs to the CodeIgniter loader, more details in the "Loading the library" section., (*22)

Using the set_keys method, (*23)

You can pass the keys to the set_keys methods (after loading the library) like this:, (*24)

$this->recaptcha->set_keys('site_key', 'secret_key');

Loading the library

You can load the library like any other library:, (*25)

$this->load->library('recaptcha', $config);

Or if installed via composer:, (*26)

$recaptcha = new Recaptcha($config);

And you can access the methods like this:, (*27)

$recaptcha->method_name();

The $config argument is optional, It can have an array of configs to the library., (*28)

$config options are: * $config['site_key'] - Site key provided by Google * $config['secret_key'] - Secret key provided by Google * $config['parameters'] - An associative array of parameters and their value, 'parameter-name' => 'value', more details about parameters in the "Setting parameters" section., (*29)

Setting parameters

You can set the parameters (g-recaptcha tag attributes and grecaptcha.render parameters) by using the set_parameter or set_parameters methods., (*30)

To set a parameter you can do it by calling:, (*31)

$this->recaptcha->set_parameter('parameter_name', 'value');

Or by passing an array to set_parameters:, (*32)

$this->recaptcha->set_parameters($params);

Where $params is an associative array of param_name => value., (*33)

When passing a parameter, omit the data- part, for example, If you want to set the data-theme parameter to dark you will do it like this:, (*34)

$this->recaptcha->set_parameter('theme', 'dark');

Creating the reCAPTCHA box

To create the reCAPTCHA box's HTML code call the create_box method:, (*35)

$this->recaptcha->create_box($attributes)

This method takes one optional parameter, an array of custom attributes, for example:, (*36)

$attributes = array(
    'class' => 're-box',
    'id' => 'an-id'
)

Notice: You need to have the reCAPTCHA JS code included in your code:, (*37)

<script src='https://www.google.com/recaptcha/api.js'></script>

Validating the reCAPTCHA

The is_valid method can be called to verify that the user passed the reCAPTCHA's puzzle., (*38)

$this->recaptcha->is_valid($response, $ip)

this method takes two optional parameters:, (*39)

$response - the response submitted by the user, set to NULL so that it'll be taken automatically from the POST data, (*40)

$ip - the user IP to be sent to Google's server, (*41)

Set to FALSE to not send the IP, (*42)

Set to NULL to get the user's IP automatically, (*43)

And it returns an array:, (*44)

'success' => TRUE if the recaptcha was passed,

'error' => TRUE if there was an error connecting to the server,

'error_message' => If error is true, this contains the message returned by curl,

'challenge_ts' =>  timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)

'hostname' => the hostname of the site where the reCAPTCHA was solved

'error-codes' => error codes returned by Google if there are any

Example

Here is a quick example to use the Codeigniter-recaptcha library., (*45)

Installed via composer

The Controller, (*46)

create_box();

        // Check if the form is submitted
        if($this->input->post('action') === 'submit')
        {
            /*
             Check if the reCAPTCHA was solved
             You can pass arguments to the `is_valid` method,
             but it should work fine without any.
             Check the "Validating the reCAPTCHA" section for more details
            */
            $is_valid =$recaptcha->is_valid();

            if($is_valid['success'])
            {
                echo "reCAPTCHA solved";
            }
            else
            {
                echo "reCAPTCHA not solved/an error occured";
            }
        }

        $this->load->view('form', ['recaptcha' => $box]);
    }
```

### Installed manually

**The Controller**
```php
load->library('recaptcha');

        /*
         Create the reCAPTCHA box.
         You can pass an array of attributes to this method.
         Check the "Creating the reCAPTCHA box" section for more details
        */
        $recaptcha = $this->recaptcha->create_box();

        // Check if the form is submitted
        if($this->input->post('action') === 'submit')
        {
            /*
             Check if the reCAPTCHA was solved
             You can pass arguments to the `is_valid` method,
             but it should work fine without any.
             Check the "Validating the reCAPTCHA" section for more details
            */
            $is_valid = $this->recaptcha->is_valid();

            if($is_valid['success'])
            {
                echo "reCAPTCHA solved";
            }
            else
            {
                echo "reCAPTCHA not solved/an error occured";
            }
        }

        $this->load->view('form', ['recaptcha' => $recaptcha]);
    }
```
---

**The view**

```html



    CodeIgniter reCAPTCHA
    
    



    
=$recaptcha?> <button type="submit" name="action" value="submit">Submit</button> </form> </body> </html>

Contributing

All contributions are welcome! Just make sure you read How to contribute, (*47)

The Versions

18/05 2018

v1.1.0.x-dev

1.1.0.9999999-dev https://github.com/mehdibo/Codeigniter-recaptcha

CodeIgniter library to use Google's reCAPTCHA V2

  Sources   Download

MIT

The Requires

  • php >=5.6

 

codeigniter recaptcha

18/05 2018

dev-develop

dev-develop https://github.com/mehdibo/Codeigniter-recaptcha

CodeIgniter library to use Google's reCAPTCHA V2

  Sources   Download

MIT

The Requires

  • php >=5.6

 

codeigniter recaptcha

18/05 2018

dev-master

9999999-dev https://github.com/mehdibo/Codeigniter-recaptcha

CodeIgniter library to use Google's reCAPTCHA V2

  Sources   Download

MIT

The Requires

  • php >=5.6

 

codeigniter recaptcha

18/05 2018

v1.1.0

1.1.0.0 https://github.com/mehdibo/Codeigniter-recaptcha

CodeIgniter library to use Google's reCAPTCHA V2

  Sources   Download

MIT

The Requires

  • php >=5.6

 

codeigniter recaptcha