【问题标题】:PHP Form with recaptcha带有recaptcha的PHP表单
【发布时间】:2015-04-21 10:51:53
【问题描述】:

我正在使用带有 re-captcha 的 php 联系表单,但不知道应该使用什么值:

$client_captcha_response = $_POST['g-recaptcha-response'];

将在 g-recaptcha-response 中,因为我试图通过另一个验证代码页收集此信息,但每次我提交时都会为 g-recaptcha-response 获得不同的值

我的提交 id 上的表单显示一个空白页面。

HTML 端代码:

    <form method="post" action="contactform.php">
  <label>Your Name (required):</label>
    <input name="name" type="text" placeholder="Enter your name here">
  <label>Email Address (required):</label>
    <input name="email" type="email" placeholder="Enter your email address here">
  <label>Your Message (required):</label>
    <textarea name="message" placeholder="Write your message here"></textarea>
  <div class="g-recaptcha" data-sitekey="6LfTVfgSAAAAADvdpWzYkifBXTmJ8ohokl_lIKgH"></div>
  <input id="submit" name="submit" type="submit" value="Submit Form">
</form>

PHP 端代码:

<?php
$your_secret = "<Secret key>";
$client_captcha_response = $_POST['g-recaptcha-response'];
$user_ip = $_SERVER['REMOTE_ADDR'];

$captcha_verify = open_https_url("https://www.google.com/recaptcha/api/siteverify?secret=$your_secret&response=$client_captcha_response&remoteip=$user_ip");
$captcha_verify_decoded = json_decode($captcha_verify);
if(!$captcha_verify_decoded->success)
  die('DIRTY ROBOT');

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = $_POST['human'];
$from = 'From: My Website';
$to = 'myemail@gmail.com';
$subject = 'Request Form';

$body = "Name: $name \n E-Mail: $email \nMessage:\n$message";

if ($_POST['submit']) {
    if ($email != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
                echo '<p>You have successfully submitted your information to PS4RS. Subscribers to our mailing list will begin to periodically receive updates.</p>';
            } else { 
                echo '<p>Something went wrong, go back and try again!</p><p><input type="button" value="Go Back" onclick="history.back(-1)" class="goback" /></p>'; 
            } 
        } else if ($_POST['submit'] && $human != '4') {
            echo '<p>You answered the anti-spam question incorrectly!</p><p><input type="button" value="Go Back" onclick="history.back(-1)" class="goback" /></p>';
        }
    } else {
        echo '<p>You need to fill in all required fields!!</p><p><input type="button" value="Go Back" onclick="history.back(-1)" class="goback" /></p>';
    }
}
?>

【问题讨论】:

  • g-recaptcha-response 如何发布到服务器?我没有在您的表单中看到具有该名称的输入。
  • 其实我对此感到困惑。
  • 那么您需要在表单中使用该名称输入字段,以便用户输入验证码,对吧?比如:&lt;input name="g-recaptcha-response" type="text" placeholder="Enter Captcha" /&gt;
  • 是的,我同意让我试试这个。
  • 武士,这个怎么样:
    因为我的表单已经显示没有“" 我遇到的问题是提交后我得到一个空白页:(

标签: php forms recaptcha


【解决方案1】:

这是我的代码运行没有问题:

客户端:

<div class="g-recaptcha" data-sitekey="PUBLIC_KEY"></div>

服务器端:

if (isset($_POST['g-recaptcha-response'])) {
    $captcha = $_POST['g-recaptcha-response'];
    $privatekey = "SECRET_KEY";
    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $data = array(
        'secret' => $privatekey,
        'response' => $captcha,
        'remoteip' => $_SERVER['REMOTE_ADDR']
    );

    $curlConfig = array(
        CURLOPT_URL => $url,
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => $data
    );

    $ch = curl_init();
    curl_setopt_array($ch, $curlConfig);
    $response = curl_exec($ch);
    curl_close($ch);
}

$jsonResponse = json_decode($response);

if ($jsonResponse->success == "true")
    doSomething();
else
    doSomeOtherThing();

工作here! :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2019-01-19
    • 2020-02-16
    相关资源
    最近更新 更多