phelium/recaptcha

reCAPTCHA v2 类

1.0.8 2018-05-13 13:22 UTC

This package is auto-updated.

Last update: 2024-08-28 21:00:28 UTC


README

安装

使用Composer,在require部分添加此行

"phelium/recaptcha": "dev-master"

然后运行composer update

初始化

require 'vendor/autoload.php';

use Phelium\Component\reCAPTCHA;

要初始化reCAPTCHA,您必须提供您的站点密钥和您的密钥。
有两种可能的方式

$reCAPTCHA = new reCAPTCHA('your site key', 'your secret key');

$reCAPTCHA = new reCAPTCHA();
$reCAPTCHA->setSiteKey('your site key');
$reCAPTCHA->setSecretKey('your secret key');

使用

要生成script标签,使用

$reCAPTCHA->getScript();

要在表单中使用HTML块,使用

$reCAPTCHA->getHtml();

在服务器端检查,在您的表单验证脚本中

if ($reCAPTCHA->isValid($_POST['g-recaptcha-response']))
{
	// do whatever you want, the captcha is valid
}
else
{
	// Show errors
	var_dump($reCAPTCHA->getErrorCodes());
}

定制

主题

有几种主题可供选择:浅色(默认)或深色。

$reCAPTCHA->setTheme('dark');

语言

您可以更改reCAPTCHA的语言。更多信息请查看https://developers.google.com/recaptcha/docs/language
默认情况下,语言会自动检测。

$reCAPTCHA->setLanguage('it');

类型

有几种类型可供选择:图片(默认)或音频。

$reCAPTCHA->setType('audio');

大小

有两种大小可供选择:常规(默认)或紧凑。

$reCAPTCHA->setType('compact');

完整示例

以下是一个示例

<?php
require 'vendor/autoload.php';
use Phelium\Component\reCAPTCHA;

$reCAPTCHA = new reCAPTCHA('your site key', 'your secret key');
?>

<html>
<head>
    <title>reCAPTCHA example</title>
    <?php echo $reCAPTCHA->getScript(); ?>
</head>

<body>

<?php
if (isset($_POST['name']))
{
    var_dump($_POST);

    if ($reCAPTCHA->isValid($_POST['g-recaptcha-response']))
    {
        echo '<br>-- Captcha OK ! --<br>';
    }
}
?>

<form action="#" method="POST">
    <input type="text" name="name" placeholder="name">

    <?php echo $reCAPTCHA->getHtml(); ?>

    <input type="submit">
</form>

</body>
</html>