我认为验证码 PHP 验证不起作用


recaptcha PHP validation not working i think

好的,请耐心等待。

我已经像这样设置了我的 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>

我的验证码出现了,我可以输入单词,我将公钥和私钥放在正确的位置以及所有内容。目前为止,一切都好。

我的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 文件中验证验证码。问题是我不知道该怎么做。

在这一点上,我只是一个Web开发专业的学生,我正在尝试一些我以前从未尝试过的东西 - 我们已经看到了BASIC联系表格和基本的PHP,但我们还没有处理复选框,内爆或验证 - 到目前为止,我已经自己解决了这一切。

有人可以看看我的代码并告诉我如何将验证函数放在我的 PHP 文件中吗?非常欢迎提供详细信息和细节。

在过去的几天里,我尝试自己搜索这些信息,但我仍然不明白 - 我还无法修改我找到的答案以满足我的需求。我还在学习。但我确实需要这样做,您的帮助将不胜感激。

有没有善良的灵魂愿意怜悯一个热切的n00b?

根据此页面,您需要顶部的验证码代码。

试试这个(未经测试(:

<?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'">";
        }
    }
?>

要在 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)