recaptcha PHP验证不起作用我想

时间:2013-09-30 19:54:19

标签: php html forms validation recaptcha

好的,请耐心等待我。

我的HTML表单代码设置如下......

<script type="text/javascript">
     var RecaptchaOptions = {
        theme : 'clean'
     };
</script>                                   
<form method="post" action="signup-form-1.php">
    <label for="Name">Name:</label><br />
    <input type="text" name="Name" id="Name" placeholder="Your Name" required="required" /><br /><br />

    <label for="Email">Email Address:</label><br />
    <input type="email" name="Email" id="Email" placeholder="Your E-mail address" required="required" /><br /><br />

     <label for="Phone">Phone Number</label>
    <input type="tel" name="Phone" id="tel" placeholder="Numbers and Hyphens allowed" required="required" /><br /><br />

    <!--<label for="Subject">Phone Number:</label><br />
    <input type="text" name="Subject" id="Subject" /><br /><br />-->

    <label for="Age">Age:</label><br />
    <input type="text" name="Age" id="Age" placeholder="How Old Are You?" required="required" /><br /><br />

    <label for="InterestedIn">What Tournament are you interested in?</label><br /><br />
        <input type="checkbox" name="InterestedIn[]" value="op1"><p class="check1">option 1</p>
        <input type="checkbox" name="InterestedIn[]" value="op2"><p class="check1">option 2</p>
        <input type="checkbox" name="InterestedIn[]" value="op3"><p class="check1">option 3</p>
        <input type="checkbox" name="InterestedIn[]" value="op4"><p class="check1">option 4</p>


    <label for="Message">Message:</label><br />
    <textarea name="Message" rows="20" cols="20" id="Message" placeholder="Additional Information"></textarea><br /><br />

    <script type="text/javascript" src="http://api.recaptcha.net/challenge?k=recaptcha key 1"></script>
    <noscript>
        <iframe src="http://api.recaptcha.net/noscript?k=recaptcha key 2" height="300" width="500" frameborder="0" title="CAPTCHA test"></iframe>
        <br />
        <label for="tswcaptcha">Copy and paste the code provided in above box here:</label><br />
        <textarea name="recaptcha_challenge_field" id="tswcaptcha" rows="3" cols="40"></textarea>
        <input type="hidden" name="recaptcha_response_field" value="manual_challenge" />
    </noscript>

    <input type="submit" name="submit" value="Submit" class="submit-button" />
    <input type="reset" name="reset" value="Reset" class="reset-button" />

</form>

我的recaptcha出现了,我能够输入单词,我已经将公钥和私钥放在正确的位置和所有内容中。到现在为止还挺好。

我的PHP代码看起来像这样......

<?php


$EmailFrom = Trim(stripslashes($_POST['Name'])); 
$EmailTo = "my.email@mail.com";
$Subject = 'Custom Subject Line here';
$Name = Trim(stripslashes($_POST['Name'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Phone = Trim(stripslashes($_POST['Phone'])); 
$Age = Trim(stripslashes($_POST['Age'])); 
$Message = Trim(stripslashes($_POST['Message'])); 
$InterestedIn = Trim(stripslashes($_POST['InterestedIn'])); 


// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body .= "Age: ";
$Body .= $Age;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$Body .= "InterestedIn: ";
$Body .= implode(", ", $_POST['InterestedIn']);
$Body .= "\n";


// send email 
$success = mail($EmailTo, $Subject, $Body, "From: $Name");



// redirect to success page 
if ($success){
  print "<meta http-

equiv=\"refresh\" content=\"0;URL=signup-thanks.htm\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=signup-error.htm\">";
}

?>

我的PHP现在解析数据并以电子邮件格式发送。我可以收到它,我甚至用逗号分隔复选框选项,一切都没问题。

我遇到的唯一问题是即使重新填写没有填写,表格仍然会发送。我知道我需要在PHP文件中验证recaptcha。问题是我不知道该怎么做。

我现在只是一名网络开发学生,我正在尝试以前从未尝试过的东西 - 我们已经展示了基本的联系表单和基本的PHP,但我们还没有处理复选框,内爆或验证 - 到目前为止,我已经完全理解了这一切。

有人在那里请看看我的代码并告诉我如何将验证函数放在我的PHP文件中?非常欢迎细节和细节。

在过去的几天里,我尝试自己搜索这些信息,但我仍然不明白 - 我无法修改我认为符合我需要的答案。我还在学习。但我确实需要这样做,非常感谢你的帮助。

任何善良的灵魂都愿意怜悯n00b?

2 个答案:

答案 0 :(得分:1)

根据this page,您需要重新计算代码。

试试这个(未经测试):

<?php
    require_once('recaptchalib.php');
    $privatekey = "your_private_key";
    $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

    if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
    } else {

        $EmailFrom = Trim(stripslashes($_POST['Name'])); 
        $EmailTo = "my.email@mail.com";
        $Subject = 'Custom Subject Line here';
        $Name = Trim(stripslashes($_POST['Name'])); 
        $Email = Trim(stripslashes($_POST['Email'])); 
        $Phone = Trim(stripslashes($_POST['Phone'])); 
        $Age = Trim(stripslashes($_POST['Age'])); 
        $Message = Trim(stripslashes($_POST['Message'])); 
        $InterestedIn = Trim(stripslashes($_POST['InterestedIn'])); 


        // prepare email body text
        $Body = "";
        $Body .= "Name: ";
        $Body .= $Name;
        $Body .= "\n";
        $Body .= "Email: ";
        $Body .= $Email;
        $Body .= "\n";
        $Body .= "Phone: ";
        $Body .= $Phone;
        $Body .= "\n";
        $Body .= "Age: ";
        $Body .= $Age;
        $Body .= "\n";
        $Body .= "Message: ";
        $Body .= $Message;
        $Body .= "\n";
        $Body .= "InterestedIn: ";
        $Body .= implode(", ", $_POST['InterestedIn']);
        $Body .= "\n";


        // send email 
        $success = mail($EmailTo, $Subject, $Body, "From: $Name");



        // redirect to success page 
        if ($success){
          print "<meta http-

        equiv=\"refresh\" content=\"0;URL=signup-thanks.htm\">";
        }
        else{
          print "<meta http-equiv=\"refresh\" content=\"0;URL=signup-error.htm\">";
        }
    }
?>

答案 1 :(得分:0)

要在HTML端解决此问题,您可以使用required attr。

示例:

<input type="hidden" name="recaptcha_response_field" value="manual_challenge" required/>

要在PHP端解决此问题,您可以使用isset和其他字符串控件

示例:

if(isset($_GET['recaptcha_response_field']) && $_GET['recaptcha_response_field'] != "" && strlen($_GET['recaptcha_response_field']) > 5)