联系表格与reCaptcha

时间:2016-01-01 20:31:33

标签: php html5 forms recaptcha contact-form

我知道之前可能已经提交过了(抱歉) 我有基本形式,这些是要发送的详细信息,但是我无法让reCaptcha使用它。我整天用谷歌搜索,但是当我尝试其他人的代码(修改以适应我的代码)时,它似乎无法工作。

我想:姓名,电子邮件,电话号码,简讯(是/否)和重新发送/工作。

有人可以告诉我可能出错的地方吗?我可能需要添加什么?

提前致谢!

这是我的表格(html)

<form method="POST" action="Form_Activation.php">
   <div class="form-group">
        <label for="name">Name:</label>
            <input type="text" class="form-control" id="name" name="name" placeholder="Full Name"       value="" required/>
        </div>
        <div class="form-group">
            <label for="email">Email:</label>
            <input type="email" class="form-control" id="email" name="email" value=""       placeholder="you@example.com" required/>
        </div>
        <div class="form-group">
            <label for="number">Number:</label>
            <input class="form-control" name="number" id="number" value="" placeholder="Contact         Number" required/>
        </div>
        <div class="form-group">
            <label for="message">Message:</label>
            <textarea class="form-control" name="message" id="message" placeholder="Enter       Message.." required></textarea>
        </div>
        <div class="form-group">
            <input type="checkbox"/> <b> Subscribe to Newsletter</b>
        </div>
        <div class="g-recaptcha" data-sitekey="6Le2SBQTAAAAADIOrUEPpcEVvR_c0vN9GzQpLg05"></div>
        <button type="submit" class="btn btn-default sendbutton">SEND</button>
        </form>

这是我的php(基本)

    <?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $number = $_POST['number'];
    $message = $_POST['message'];

    //$password = $_POST['password'];

    //$keyy = $_SERVER['UNIQUE_ID'];


    $msg = "Name: $name\r\n \r\n"; 

    $msg .= "Email: $email\r\n \r\n";

    $msg .= "Number: $number\r\n \r\n"; 

    $msg .= "Message: $message\r\n \r\n"; 

    $recipient = "info@islandwebdesign.co.uk";
    $subject = "New Website Request";
    $mailheaders = "From:$email";
    //$mailheaders .= "Reply-To:$email";
    mail($recipient,$subject,$msg,$mailheaders);
    header("Location: contactus.php?msg=1");
    ?>

1 个答案:

答案 0 :(得分:0)

首先请确保您已正确包含必要的JavaScript资源以正确呈现reCAPTCHA小部件,如下所示:

<html>
  <head>
    <title>reCAPTCHA demo: Simple page</title>
     <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  </head>
  <body>
    <form action="?" method="POST">
      <div class="g-recaptcha" data-sitekey="your_site_key"></div>
      <br/>
      <input type="submit" value="Submit">
    </form>
  </body>
</html>

以下是参考资料:

现在来到您用户的回复中。用户验证码挑战的响应可以通过三种方式获取。它可以是,

现在来到您用户的回复中。用户验证码挑战的响应可以通过三种方式获取。它可以是,

  • g-recaptcha-response - 提交表单中的POST参数
  • grecaptcha.getResponse(widget_id) - 将在用户完成验证码后提供响应。
  • 传递给render方法的config对象中指定的回调函数的字符串参数。

以下是参考资料:

为了您的目的,使用 g-recaptcha-response 来获取用户的回复。所以你的代码应该是这样的:

<强> HTML

<form method="POST" action="Form_Activation.php">
   <div class="form-group">
    <label for="name">Name:</label>
        <input type="text" class="form-control" id="name" name="name" placeholder="Full Name" value="" required/>
    </div>
    <div class="form-group">
        <label for="email">Email:</label>
        <input type="email" class="form-control" id="email" name="email" value="" placeholder="you@example.com" required/>
    </div>
    <div class="form-group">
        <label for="number">Number:</label>
        <input class="form-control" name="number" id="number" value="" placeholder="Contact Number" required/>
    </div>
    <div class="form-group">
        <label for="message">Message:</label>
        <textarea class="form-control" name="message" id="message" placeholder="Enter Message.." required></textarea>
    </div>
    <div class="form-group">
        <input type="checkbox"/> <b> Subscribe to Newsletter</b>
    </div>
    <div class="g-recaptcha" data-sitekey="6Le2SBQTAAAAADIOrUEPpcEVvR_c0vN9GzQpLg05"></div>
    <button type="submit" name="submit" class="btn btn-default sendbutton">SEND</button>
</form>

在提交按钮中添加name属性。

<强> Form_Activation.php

<?php

    if(isset($_POST['submit'])){

        //your site secret key
        $secret = 'XXXXXXX_Secret-key_XXXXXXX';

        if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
            //get verified response data
            $param = "https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$_POST['g-recaptcha-response'];
            $verifyResponse = file_get_contents($param);
            $responseData = json_decode($verifyResponse);

            if($responseData->success){
                // success

                $name = $_POST['name'];
                $email = $_POST['email'];
                $number = $_POST['number'];
                $message = $_POST['message'];

                // so on

            }else{
                // failure
            }

        }

    }

?>

不要忘记在$secret变量中添加密钥。