reCAPTCHA表单返回错误

时间:2015-03-26 08:51:04

标签: php forms recaptcha

我的表单中包含一个简单的Recaptcha,并且它给了我这个错误:Use of undefined constant success - assumed 'success'

我查看了文档,并且已经包含了集成的所有必要组件。当我提交表单时,我仍然收到同样的错误。

感谢您的帮助! :)

这是我的代码:

PHP:

// reCAPTCHA
$captcha = isset($_POST['g-recaptcha-response']) ? $_POST['g-recaptcha-response'] : "";

if (empty($captcha)) {
    echo "Please check the reCAPTCHA.";
}
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=removed_for_demonstration&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
if ($response.success == true) {
    echo "It worked";
}

HTML:

<form action="contact.php" method="post">
    <input name="name" type="text" class="form-control" />
    <div class="g-recaptcha" data-sitekey="removed_for_demonstration"></div>
    <input type="submit" name="contact" value="SUBMIT GAME" />
</form>

2 个答案:

答案 0 :(得分:3)

Google的reCaptcha响应是一个JSON对象。如果要访问数组中的值,则需要使用json_decode解码响应。

$decoded_response = json_decode($response, true)
if ($decoded_response["success"] == true) {
    //Do Stuff
}

编辑:

您还可以访问数据OOP样式:

$decoded_response = json_decode($response)
if ($decoded_response->success) {
    //Do Stuff
}

答案 1 :(得分:0)

试试这个:

更改

if ($response.success == true) {

要:

if ($response ['success']) == true) {