验证码。需要一些PHP脚本来将recaptcha集成到现有表单。这可能是一个简单的答案

时间:2012-01-21 19:48:44

标签: php recaptcha

我不是那些经验丰富的html和php,但我设法创建了一个联系表单和php脚本,用于向我的hotmail帐户发送电子邮件,它一直运行良好。

现在我想将Recaptcha添加到该表单中并成功完成。但现在我想在下一个主题上提供一些帮助:

  • 我想用表单
  • 进行recpatcha
  • 当recaptcha不正确时我想保留信息字段而不发送电子邮件也不显示“感谢您的邮件已被发送”。
  • 仅当recaptcha正确时:发送电子邮件,清除信息字段,显示“感谢信息已发送”

抱歉我忘记了这篇文章的全部内容都在一个单一的网页上“CONTACTFORM.PHP” 顺便说一句,这是她。

非常感谢您的回答!如果你能给出答案的一些细节就会很棒!!!

这里我有我的代码:

任何帮助都将非常感谢!

这是我发送电子邮件的PHP:

<?php
if ($_POST['parse_var'] == "form1"){

    $emailTitle = 'Email from my website!';
    $emailAddress = 'myemail@hotmail.com';

    /* Gathering Data Variables */

    $emailField = $_POST['email'];
    $phoneField = $_POST['phone'];
    $nameField = $_POST['name'];
    $subjectField = $_POST['subject'];
    $messageField = $_POST['message'];

    $body = <<<EOD
<br><hr><br>
Email: $emailField <br />
Phone: $phoneField <br />
Name: $nameField <br />
Subject: $subjectField <br />
Message: $messageField <br />
EOD;

    $headers = "From: $emailField\r\n";
    $headers .= "Content-type: text/html\r\n";
    $success = mail("$emailAddress", "$emailTitle", "$body", "$headers");

    $sent = "Thank you! Your message has been sent."; 
}
?> 

 <form id="form1" name="form1" method="post" action="contactform.php">
      <table width="100%" border="0" cellspacing="6" cellpadding="0">
        <tr>
          <td align="right"><label for="email" class="titulos">Email: </label></td>
          <td align="left"><input name="email" type="text" id="email" size="35" maxlength="50" /> 
            *</td>
        </tr>
        <tr>
          <td align="right"><label for="phone" class="titulos">Phone: </label></td>
          <td align="left"><input name="phone" type="text" id="phone" size="35" maxlength="50" /></td>
        </tr>
        <tr>
          <td align="right"><label for="name" class="titulos">Name:</label></td>
          <td align="left"><input name="name" type="text" id="name" size="35" maxlength="30" />
            *</td>
        </tr>
        <tr>
          <td align="right"><label for="subject" class="titulos">Subject:</label></td>
          <td align="left"><input name="subject" type="text" id="subject" size="35" maxlength="60" /></td>
        </tr>
        <tr>
          <td align="right" valign="top"><label for="message" class="titulos">Message:</label></td>
          <td align="left"><p>
            <textarea name="message" cols="27" rows="5" id="message"></textarea>
            *</p></td>
        </tr>
        <tr>
          <td align="right">&nbsp;</td>
          <td align="left">
<?php
if (isset($_POST['send'])) {
    require_once('recaptchalib.php');
    $privatekey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

   if (!$resp->is_valid) {
      echo "Please try again";
   } else {
      echo "Correct"; 
   }
}
?>
<?php
    require_once('recaptchalib.php');
    $publickey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // you got this from the signup page
    echo recaptcha_get_html($publickey);
?>
</td>
        </tr>
        <tr>

          <td align="right"><label for="reset"></label></td>
          <td align="left"><label for="send"></label>
            <input type="hidden" name="parse_var" id="parse_var" value="form1" />

            <input name="send" type="submit" id="send" onclick="MM_validateForm('email','','RisEmail','name','','R','message','','R');return document.MM_returnValue" value="Send Email" /> <label for="reset"></label>
            <input type="reset" name="reset" id="reset" value="Reset form" /></td>
        </tr>
        <tr>
          <td align="right">&nbsp;</td>
          <td align="left" class="content">(*) Are Required fields</td>
        </tr>
        <tr>
          <td colspan="2" align="center"><?php print "$sent"; ?> 



         </td>
          </tr>
  </table>
</form>

1 个答案:

答案 0 :(得分:2)

首先了解PHP SESSIONS

这是你的算法:

  1. 使用recaptcha(form.php)
  2. 显示您的表单
  3. 将表单发送至step2.php以检查recaptcha,如果正确,请发送电子邮件。如果错误,请将电子邮件,电话,姓名,主题和消息设置为会话(例如:$_SESSION["name"] = $_POST["name"];)(您必须在step2.php的顶部开始与session_start();的会话)并重定向到带有查询字符串的表单页面。像这样:form.php?error=r
  4. 在form.php上查询r查询字符串。如果有,则打印错误 Wrong Rechaptcha并将所有<input>的默认值设置为 $_SESSION值。 ($_SESSION["name"]
  5. 如果你想在一页上做(推荐)

    <?php
    if ($_POST["submit"])  // If form submitted
    {
    // recaptcha control. If pass, record it to database, or send mail, or whatever you want.
    }
    ?>
    <form action="" method="POST">
    <input value="<?php echo $_POST['name']; ?>" name="name" type="textbox">
    <input type="submit" name="submit">
    </form>