验证失败后Google重新加载页面没有重新加载?

问题描述 投票:0回答:1

我已经在我的form.php中实现了recaptcha v2,但是当我点击提交而没有验证验证码时,页面重新加载显示回复响应请返回并确保检查安全验证码框。但验证码形式不再可见,但在HTML中?

我已经尝试过grecaptcha.reset()但仍然无法使其工作

代码如下

提前致谢

HTML:

    <?php
    include ('form_process.php');

    ?>

    <div class="grey">
            <div class="container-contact">
                <form id="contact" method="post">
                    <div id="column-contact-left">
                    <div class='contact-logo'></div>
                    <h3>Contact the Devon Food Movement</h3>
                    <fieldset id="field-no-ui">
                        <input placeholder="Your name" type="text" tabindex="1" name="name1" value="<?= $name ?>" >

                    </fieldset>
                    <span class="error"><?= $name_error ?></span>
                    <fieldset id="field-no-ui">
                        <input placeholder="Your Email Address" type="text" name="email" value="<?= $email ?>" tabindex="2" >

                    </fieldset>
                    <span class="error"><?= $email_error ?></span>
                    </div>
                    <div id="column-contact-right">
                    <fieldset id="field-no-ui">
                        <textarea id="field-no-ui" placeholder="Type your Message Here...." name="message" value="<?= $message ?>" tabindex="3" ></textarea>
                    </fieldset>
                    <div class="g-recaptcha" data-sitekey="6LfJtkcUAAAAAE_7Ob_7BVMkaZMXX-dH-V6uqjCn" ></div>
                    <span class="sent"><?= $sent; ?></span>
                    <fieldset id="field-no-ui">
                        <button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
                    </fieldset>
                    </div>
                </form>
            </div>
    </div>

form_process.php

    <?php

    function post_captcha($user_response) {
        $fields_string = '';
        $fields = array(
            'secret' => 'secret',
            'response' => $user_response
        );
        foreach($fields as $key=>$value)
            $fields_string .= $key . '=' . $value . '&';

        $fields_string = rtrim($fields_string, '&');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

        $result = curl_exec($ch);
        curl_close($ch);

        return json_decode($result, true);
    }

    $res = post_captcha($_POST['g-recaptcha-response']);

    $name_error = $email_error = "";
    $name = $email = $message = $sent = "";

    if (isset($_POST['submit']) AND (!$res['success'])) {
        // What happens when the CAPTCHA wasn't checked
        echo '<p style="margin-top: 0">Please go back and make sure you check the security CAPTCHA box.</p><br>';
    } else if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["name1"])) {
            $name_error = "Name is required";
        } else {
            $name = test_input($_POST["name1"]);
            // check if name only contains letters and whitespace

            if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                $name_error = "Only letters and white space allowed";
            }
        }

        if (empty($_POST["email"])) {
            $email_error = "Email is required";
        } else {

            $email = test_input($_POST["email"]);
            // check if e-mail address is well-formed
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $email_error = "Invalid email format";
            }
        }

        if (empty($_POST["message"])) {
            $message = "";
        } else {
            $message = test_input($_POST["message"]);
        }

        if ($name_error == '' and $email_error == '' ){
            $message_body = '';
            unset($_POST['submit']);

            foreach ($_POST as $key => $value){
                $message_body .=  "$key: $value\n";
            }

            $email = $_POST['email'];
            $to = '@gmail.com';
            $subject = 'Contact Form Submit';
            $headers = 'From:' . $email . "\n" . 'Reply-to: ' . $email . "\n"  ;
            if (mail($to, $subject, $message, $headers)) {
                $sent = "Message sent";
                $name = $email = $message = '';
            }
        }
    }
    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    ?>
php recaptcha
1个回答
0
投票

我将recaptcha的脚本移动到form.php文件中并直接放在recaptcha div下,现在每次都加载recaptcha

© www.soinside.com 2019 - 2024. All rights reserved.