javascript - reCAPTCHA PHP and AJAX - Stack Overflow

I am trying to add a reCAPTCHA to my PHP contact form with AJAX. I want the user to be displayed with t

I am trying to add a reCAPTCHA to my PHP contact form with AJAX. I want the user to be displayed with the reCAPTCHA and have the user enter it. If it is incorrect it should display so without leaving the page. Only if the CAPTCHA is correct should it go to contacts-go.php and run the code to send the e-mail. I tried to do it from this example.

This is what I have on my contact.php page:

<?php   
    session_start();
?>

<html>
    <head>
        <script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
        <SCRIPT type="text/javascript">
            function validateCaptcha()
            {
                challengeField = $("input#recaptcha_challenge_field").val();
                responseField = $("input#recaptcha_response_field").val();
                //alert(challengeField);
                //alert(responseField);
                //return false;
                var html = $.ajax({
                type: "POST",
                url: "ajax.recaptcha.php",
                data: "recaptcha_challenge_field=" + challengeField + "&amp;recaptcha_response_field=" + responseField,
                async: false
                }).responseText;

                if(html == "success")
                {
                    $("#captchaStatus").html(" ");
                    // Unment the following line in your application
                    return true;
                }
                else
                {
                    $("#captchaStatus").html("Your captcha is incorrect. Please try again");
                    Recaptcha.reload();
                    return false;
                }
            }

        </SCRIPT>

        <script type="text/javascript">

</script>

    </head>


<body>
    <?php
        require_once("ajax.recaptcha.php");
    ?>

    <div id="login">
        <form method="post" onSubmit="return validateCaptcha()" action="contacts-go.php">

                <label>Message</label>
                <br />
                <textarea type="text" id="name" name="message" size="20"></textarea>

            <p><?php echo recaptcha_get_html($publickey);?></p>

            <p style="color: red;" id="captchaStatus">&nbsp;</p>

            <input type=submit name="submit" value="Submit">
        </form>
    </div>

</body>
</html>

This is the ajax.recaptcha.php file

<?php

    //A. Load the Recaptcha Libary
    require_once("recaptchalib.php");
    require_once("recaptha-keys.php");  // my reCAPTCHA Keys

    $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

    if ($resp->is_valid) {
        ?>success<?
        $_SESSION['captcha'] = 1;
    }
    else 
    {
        die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
           "(reCAPTCHA said: " . $resp->error . ")");
    }
?>

All I am getting is a blank page with the following:

The reCAPTCHA wasn't entered correctly. Go back and try it again.(reCAPTCHA said: incorrect-captcha-sol)

I am trying to add a reCAPTCHA to my PHP contact form with AJAX. I want the user to be displayed with the reCAPTCHA and have the user enter it. If it is incorrect it should display so without leaving the page. Only if the CAPTCHA is correct should it go to contacts-go.php and run the code to send the e-mail. I tried to do it from this example.

This is what I have on my contact.php page:

<?php   
    session_start();
?>

<html>
    <head>
        <script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
        <SCRIPT type="text/javascript">
            function validateCaptcha()
            {
                challengeField = $("input#recaptcha_challenge_field").val();
                responseField = $("input#recaptcha_response_field").val();
                //alert(challengeField);
                //alert(responseField);
                //return false;
                var html = $.ajax({
                type: "POST",
                url: "ajax.recaptcha.php",
                data: "recaptcha_challenge_field=" + challengeField + "&amp;recaptcha_response_field=" + responseField,
                async: false
                }).responseText;

                if(html == "success")
                {
                    $("#captchaStatus").html(" ");
                    // Unment the following line in your application
                    return true;
                }
                else
                {
                    $("#captchaStatus").html("Your captcha is incorrect. Please try again");
                    Recaptcha.reload();
                    return false;
                }
            }

        </SCRIPT>

        <script type="text/javascript">

</script>

    </head>


<body>
    <?php
        require_once("ajax.recaptcha.php");
    ?>

    <div id="login">
        <form method="post" onSubmit="return validateCaptcha()" action="contacts-go.php">

                <label>Message</label>
                <br />
                <textarea type="text" id="name" name="message" size="20"></textarea>

            <p><?php echo recaptcha_get_html($publickey);?></p>

            <p style="color: red;" id="captchaStatus">&nbsp;</p>

            <input type=submit name="submit" value="Submit">
        </form>
    </div>

</body>
</html>

This is the ajax.recaptcha.php file

<?php

    //A. Load the Recaptcha Libary
    require_once("recaptchalib.php");
    require_once("recaptha-keys.php");  // my reCAPTCHA Keys

    $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

    if ($resp->is_valid) {
        ?>success<?
        $_SESSION['captcha'] = 1;
    }
    else 
    {
        die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
           "(reCAPTCHA said: " . $resp->error . ")");
    }
?>

All I am getting is a blank page with the following:

The reCAPTCHA wasn't entered correctly. Go back and try it again.(reCAPTCHA said: incorrect-captcha-sol)

Share Improve this question edited Jan 23, 2013 at 16:56 Rick asked Jan 23, 2013 at 14:52 RickRick 2,30819 gold badges67 silver badges104 bronze badges 1
  • Removing the AJAX validateCaptcha function and changing to action="ajax.recaptcha.php , I notice that it seems to get get a "success". Aperently I have something wrong in my AJAX function that keeps getting false on $resp->is_valid – Rick Commented Jan 23, 2013 at 17:49
Add a ment  | 

1 Answer 1

Reset to default 2
  • I saw the contents of recaptchalib.php

    Noticed that, the api url and verify url doesnot work.

    define("RECAPTCHA_API_SERVER", "http://api.recaptcha");
    define("RECAPTCHA_API_SECURE_SERVER", "https://api-secure.recaptcha");
    define("RECAPTCHA_VERIFY_SERVER", "api-verify.recaptcha");
    
  • Use this tutorial: http://www.jquery4u./forms/setup-user-friendly-captcha-jqueryphp/

    It uses google recaptcha.

  • More links:

    http://www.google./recaptcha/learnmore

    https://developers.google./recaptcha/docs/php

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745381464a4625245.html

相关推荐

  • javascript - reCAPTCHA PHP and AJAX - Stack Overflow

    I am trying to add a reCAPTCHA to my PHP contact form with AJAX. I want the user to be displayed with t

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信