mehdibo/codeigniter-recaptcha

用于使用Google的reCAPTCHA V2的CodeIgniter库

v1.1.1 2022-06-15 13:06 UTC

This package is auto-updated.

Last update: 2024-09-15 17:56:53 UTC


README

此库使得使用Google的reCAPTCHA V2变得简单

内容

安装

通过composer

如果你已经安装了composer,你可以运行

composer require mehdibo/codeigniter-recaptcha

config/recaptcha.php的内容复制到application/config/recaptcha.php

首次使用composer

打开终端(Windows中的命令),以下命令适用于Linux,但你也可以在Windows中找到等效命令。

  1. 你应该做的第一件事是安装composer

  2. 转到你的应用文件夹: cd application

  3. 安装库: composer require mehdibo/codeigniter-recaptcha

  4. config/recaptcha.php的内容复制到你的application/config/recaptcha.php

  5. 转到application/config/config.php并将composer_autoload设置为TRUE

  6. 这就完成了!有关更多详细信息,请参阅文档

手动安装

  1. 下载最新版本

  2. libraries/Recaptcha.php复制到application/libraries,将config/recaptcha.php复制到application/config

  3. 使用CodeIgniter加载器$this->load->library('recaptcha')加载库,查看示例

  4. 请参阅文档了解用法。

文档

获取密钥

要使用reCAPTCHA,你需要一对密钥(一个私钥和一个站点密钥),这些密钥可以通过访问https://www.google.com/recaptcha/admin从Google获取

并注册一个新的网站,确保勾选“reCAPTCHA V2”选项。

设置密钥

有三种方法将密钥传递给库

在配置文件中

你可以通过编辑config/recaptcha.php配置文件来设置密钥

使用CodeIgniter加载器

通过向CodeIgniter加载器传递配置数组,更多详细信息请参阅“加载库”部分。

使用set_keys方法

你可以将密钥传递给set_keys方法(在加载库之后),如下所示

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

加载库

你可以像加载其他库一样加载库

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

如果通过composer安装

$recaptcha = new Recaptcha($config);

你可以这样访问方法

$recaptcha->method_name();

$config参数是可选的,它可以包含传递给库的配置数组。

$config选项如下

  • $config['site_key'] - 由Google提供的站点密钥
  • $config['secret_key'] - 由Google提供的私钥
  • $config['parameters'] - 一个参数及其值的关联数组,格式为 'parameter-name' => 'value',有关参数的更多详细信息请参阅 "设置参数" 部分。

设置参数

您可以通过使用 set_parameterset_parameters 方法来设置参数(g-recaptcha 标签属性和 grecaptcha.render 参数)。

要设置参数,您可以这样做:

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

或者通过传递一个数组给 set_parameters

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

其中 $params 是一个关联数组,格式为 param_name => value

在传递参数时,请省略 data- 部分,例如,如果您想将 data-theme 参数设置为 dark,可以这样操作

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

创建reCAPTCHA框

要创建 reCAPTCHA 框的 HTML 代码,请调用 create_box 方法

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

此方法接受一个可选参数,即自定义属性数组,例如

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

注意:您需要在代码中包含 reCAPTCHA JS 代码

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

验证reCAPTCHA

可以通过调用 is_valid 方法来验证用户是否通过了 reCAPTCHA 的谜题。

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

此方法接受两个可选参数

$response - 用户提交的响应,默认设置为 NULL 以自动从 POST 数据中获取

$ip - 要发送给 Google 服务器的用户 IP

设置为 FALSE 以不发送 IP

设置为 NULL 以自动获取用户的 IP

并返回一个数组

'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

示例

以下是一个快速示例,说明如何使用 Codeigniter-recaptcha 库。

通过 composer 安装

控制器

<?php

class Form extends CI_Controller {

	public function index()
	{
		/*
		 Load the reCAPTCHA library.
		 You can pass the keys here by passing an array to the class.
		 Check the "Setting the keys" section for more details
		*/
		$recaptcha = new 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
		*/
		$box = $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 =$recaptcha->is_valid();

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

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

手动安装

控制器

<?php

class Form extends CI_Controller {

	public function index()
	{
		/*
		 Load the reCAPTCHA library.
		 You can pass the keys here by passing an array to the loader.
		 Check the "Setting the keys" section for more details
		*/
		$this->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]);
	}

视图

<!DOCTYPE html>
<html>
<head>
	<title>CodeIgniter reCAPTCHA</title>
	<!-- reCAPTCHA JavaScript API -->
	<script src='https://www.google.com/recaptcha/api.js'></script>
</head>

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

</html>

贡献

欢迎所有贡献!请确保您阅读了 如何贡献