php - ReCaptcha 不接受任何字符串

标签 php javascript mysql html recaptcha

我第一次尝试将 recaptcha 添加到联系表单,我取得了一些进展,但现在我有点卡住了。

它不允许我发送任何电子邮件,我也不知道为什么。在我遇到 recaptcha 没有阻止错误答案的问题之前,但在我将 recaptcha 代码放在表单顶部之后,它现在会自行显示并保护表单。

这是执行的代码:

<?php

        require_once('recaptchalib.php');
        $privatekey = "xxxxxxxxxxxxxxxxxxxxxx";
        $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 {
        // Your code here to handle a successful verification
        if(@mail($your_email,$email_subject,$email_content)) {
            echo 'Message sent!'; }
          }

     if(!$_POST) exit;

     $email = $_POST['email'];


        //$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i',     $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
        if(!eregi("@",$email )){
           $error.="Invalid email address entered";
           $errors=1;
        }
        if($errors==1) echo $error;
      else{
             $values = array ('name','email','telephone','message');
             $required = array('name','email','telephone','message'); 
             $your_email = "email@mydomain.com";
             $email_subject = "New Message from XXX: ".$_POST['subject'];
             $email_content = "New message:\n";

           foreach($values as $key => $value){
             if(in_array($value,$required)){
               if ($key != 'subject' && $key != 'telephone') {
                 if( empty($_POST[$value]) ) { 
                   echo 'PLEASE FILL IN REQUIRED FIELDS'; exit;
                 }
               }
                 $email_content .= $value.': '.$_POST[$value]."\n";
             }
           }

        if(@mail($your_email,$email_subject,$email_content)) {
            echo 'Message sent!'; 
        }
        else {
            echo 'ERROR!';
        }
     }
?>

下面是表单中的代码:

 <form action="./contact.php" method="post" id="contactform">
        <?php
          require_once('./recaptchalib.php');
          $publickey = "xxxxxxxxxxxxxxxxxx"; // you got this from the signup page
          echo recaptcha_get_html($publickey);
        ?>
          <ol>
            <li>
              <label for="name">Name</label>
              <input id="name" name="name" class="text" />
            </li>
            <li>
              <label for="email">Email</label>
              <input id="email" name="email" class="text" />
            </li>
            <li>
              <label for="telephone">Telephone</label>
              <input id="telephone" name="telephone" class="text" />
            </li>
            <li>
              <label for="message">Message</label>
              <textarea id="message" name="message" rows="6" cols="50"></textarea>
            </li>

            <li class="buttons">
              <input type="image" name="imageField" id="imageField" src="images/send.gif" class="send" />
              <div class="clr"></div>
            </li>
          </ol>

 </form>

感谢您的帮助!

最佳答案

“不允许我发送邮件”是什么意思?验证码是否失败并且您收到“验证码输入不正确”?

或者它是否尝试发送邮件但没有任何显示?

  1. 不要使用 eregi。整套 ereg 功能已弃用。我看到你上面有一个 preg_match 等价物,所以改用它。如果您使用的是 PHP 5.2+,则应改用 filter_var($email, FILTER_VALIDATE_EMAIL),这会比您的正则表达式做得更好。
  2. 不要用@ 抑制mail() 调用。 Mail 本身不会输出任何错误消息,它只会返回 true/false。检查该返回值,如果为假,则输出一条错误消息。目前,您仅在为真时输出。
  3. 检查您的 SMTP 服务器日志(如果您有权访问它),假设 PHP 和邮件确实在工作并尝试传递邮件。可能 SMTP 服务器出现故障,邮件卡在外发队列中

在所有这些之后,您似乎正在尝试使用 $your_email$email_subject 等发送邮件......在您设置之前这些变量并从 $_POST 中检索值。所以很可能你调用 mail() 时使用了空变量,它什么也不做。

关于php - ReCaptcha 不接受任何字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5992859/

相关文章:

php - PDO Insert 将值 1 写入具有绑定(bind)参数的所有字段

php - 如何向 MD5 地址发送电子邮件

php - 运行多个 Wordpress Cron 以执行两个函数以不同的固定时间间隔更新数据库

javascript - navigator.language 所有语言的列表

javascript - $ 在 onload 中不可用

mysql - 找不到适合 jdbc :mysql from tomcat 8. 0 的驱动程序

php - Laravel Eloquent Query 导致应用程序持续加载

javascript - PHP 不向 AJAX 返回数据

php - 将图像从 iOS 上传到 PHP

javascript - 移动到[取消]隐藏元素