reCaptcha问题,PHP通知索引未定义

时间:2017-05-21 23:34:35

标签: php forms recaptcha

我正在尝试将Googles reCaptcha添加到我的网页表单中,但我遇到了麻烦。出于某种原因,我得到了一个未定义的变量" $ captcha变量出错,"未定义索引"对于' g-recaptcha-response'。

<?php
require 'PHPMailer/PHPMailerAutoload.php';

$name = $_REQUEST['names'];
$number = $_REQUEST['cell'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$captcha = null;

if(isset($_POST['g-recaptcha-response'])){
   $captcha=$_POST['g-recaptcha-response'];
   echo 'RESPONSE RECEIVED';
}

if(!$captcha){
    echo 'RESPONSE NOT RECEIVED';
    echo '<h2>Please check the the captcha form.</h2>';
    exit;
}

以下是HTMl表格的片段:

<form  name="contact form" method="POST" action="sendemail_1.php" id="main-contact-form">
    <div class="form-group">
        <input type="text"  name="name" id="names" class="form-control" required="required" placeholder="Name">
    </div>
    <div class="form-group">
        <input type="email"  name="email" id="emails" class="form-control" required="required" placeholder="Email">
    </div>
    <div class="form-group">
        <input type="text"  name="number" id="numbers" class="form-control" required="required" placeholder="Number">
    </div>
    <div class="form-group">
        <textarea name="message" id="messages" required="required" class="form-control" rows="8" placeholder="Your text here"></textarea>
    </div>
    <div class="form-group">
        <div class="g-recaptcha" data-sitekey="6LeVTyIUAAAAAHsB-2XIbO8x59nqi2yhesv98Xo7"></div>
    </div>
    <div class="form-group">
        <input type="submit" name="submit" class="btn btn-submit" value="Submit">
    </div>
</form> 

提前谢谢!

1 个答案:

答案 0 :(得分:0)

if(!$captcha){
    echo 'RESPONSE NOT RECEIVED';
    echo '<h2>Please check the the captcha form.</h2>';
    exit;
}

应该是这个

if(!isset($_POST['g-recaptcha-response']))){
    echo 'RESPONSE NOT RECEIVED';
    echo '<h2>Please check the the captcha form.</h2>';
    exit;
}

因为如果第一个失败的话,$ captcha不存在