【问题标题】:how to give required in google captcha如何在谷歌验证码中提供所需
【发布时间】:2016-10-04 06:15:00
【问题描述】:

我的 PHP:

include_once ('db.php');

$capt_err = '';
$error = 0;

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $captcha = $_POST['g-recaptcha-response'];
    if (!$captcha) {
        echo '<script language="javascript">';
        echo 'alert("please check the captcha!");';

        // echo 'window.location.href="#mymodal";';

        echo '</script>';
        exit();
    }
    else {
        $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6LcoHioTAAAAAHvJ0FIRLC-VWVmpBSs_-7igEkXh&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
    }

    if ($response . success == false) {
        echo '<h2>You are spammer ! Get the @$%K out</h2>';
    }
    else {
        $result = mysqli_query($mysqli, "INSERT INTO vibodha_feedback (`id`,`name`,`email`,`phone`,`message`,`date`) VALUES('','$name','$email','$phone','$message',now())");
        echo "<script>" . "alert('Your Message has been sucessfully sent.')" . "</script>";
    }
}

// header('location:testimonials.php');

我的 HTML:

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"  method="post">
    <fieldset>
        <label for="name">Name:</label>
        <input type="text" id="name" name="name" required>
        <label for="mail">Email:</label>    
        <input type="email" id="email" name="email" required>
        <label for="phone">Phone:</label>
        <input type="number" id="phone" name="phone" required>
        <label for="message">Message:</label>   
        <textarea id="message" name="message" required></textarea>
        <div class="g-recaptcha" data-sitekey="6LewHioTAAAAANGO-VChqdjsoZARTVOxsrgPW6T8"></div>
    </fieldset>
    <button type="submit">Send!</button>
</form>

我的 JavaScript: 该脚本是 g-recapcha(google) 的默认脚本,google 仅集成 recapcha 选项..不提供必需的选项。

我在 php 代码中编写了验证。验证码为空警报将出现(参考 php 代码)

我的代码中的上述三个部分..我在 div 类中给出了必需的,,, div 没有被占用,那么如何在验证码中给出必需的。

【问题讨论】:

    标签: javascript php recaptcha


    【解决方案1】:

    试试这样的

     <input type="text" class="inputcontact" id="captcha_contact" placeholder="Cod securitate" name="captcha" onchange="isEmpty('captcha_contact', 'hiba-captcha_contact')">
                    <div id="error-captcha_contact"><span style="font-size: 8px;"></span></div>
    

    JS 文件:

            jQuery(document).ready(function () {
                jQuery('#contact_send').click(function () {
    var minden_ok = true;
                 if (minden_ok == true) {
                        minden_ok = isEmpty('captcha_contact', 'error-captcha_contact');
                    }
     if (minden_ok == true) {
        //your code here
      }
            });
    
    
    
    function isEmpty(ID, errorID) {
    var value= jQuery('#' + ID).val();
    if (value.length > 0) {
        jQuery("#" + errorID).hide("slow");
        jQuery("#" + ID).css("border-color", "");
        return true;
    } else {
        jQuery("#" + errorID + " span:first").text("Please fill !");
        jQuery("#" + ID).css("border-color", "red");
        jQuery("#" + errorID).show("slow");
        jQuery("#" + ID).focus();
        return false;
    }
    

    【讨论】:

    • Botond hai,,这不行,,,那该放哪里。
    • 现在检查。我添加了html内容和jquery。
    【解决方案2】:

    你可以通过 $_POST['g-recaptcha-response']; 得到它

    喜欢

    include_once ('db.php');
    
    $capt_err = '';
    $error = 0;
    
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $message = $_POST['message'];
           if($_POST['g-recaptcha-response']=='') 
           {
           echo '<script language="javascript">';
            echo 'alert("please check the captcha!");';
    
            // echo 'window.location.href="#mymodal";';
    
            echo '</script>';
            exit();
           }
    
        else {
            $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6LcoHioTAAAAAHvJ0FIRLC-VWVmpBSs_-7igEkXh&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
        }
    
        if ($response . success == false) {
            echo '<h2>You are spammer ! Get the @$%K out</h2>';
        }
        else {
            $result = mysqli_query($mysqli, "INSERT INTO vibodha_feedback (`id`,`name`,`email`,`phone`,`message`,`date`) VALUES('','$name','$email','$phone','$message',now())");
            echo "<script>" . "alert('Your Message has been sucessfully sent.')" . "</script>";
        }
    }
    
    // header('location:testimonials.php');
    

    【讨论】:

    • 我不明白!
    • 先生,我在我的代码中添加了您的代码,但是没有用,,,,,,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 2012-03-02
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    相关资源
    最近更新 更多