Jump to content

Recommended Posts

Here's my code:

 

 

<?php


include 'recaptchalib.php';


if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
    echo $_POST['g-recaptcha-response']."<p>";
    $secret = "<secret key>";
    $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
    $responseData = json_decode($verifyResponse);
    echo "Response:".$verifyResponse."<p>";
    
    if($responseData->success){
        echo "Success";
    }else{
        echo "Failed";
    }
}


?>
<!DOCTYPE HTML>
<html>
<head>
    
    <link rel="stylesheet" type="text/css" href="components/css/rs.css" />
    <script src='https://www.google.com/recaptcha/api.js'></script>


</head>
<body>
<form method="post" action="test2.php">
    <input type="text" />
    <div class="g-recaptcha" data-sitekey="<key>"></div>
    <input type="submit" />
</form>




</body>
</html>

 

 

I've tried like 7 different websites coding example... my captcha fails every time.  Their examples always work.  What else could it be?

Edited by TapeGun007
Link to comment
https://forums.phpfreaks.com/topic/303149-google-captcha/
Share on other sites

So what exactly “fails”? Be more specific.

 

Officially, Google expects a POST request. You're sending a GET request (which may or may not work). Abusing the file_get_contents() function for HTTP requests is also problematic, because many systems disable this feature for security reasons.

 

A much more robust solution would be to send a POST request with a proper HTTP library like cURL.

Link to comment
https://forums.phpfreaks.com/topic/303149-google-captcha/#findComment-1542533
Share on other sites

As I already said, use cURL. If there are still problems, turn your error reporting all the way up and post all errors/warnings/notices as well as the full response from the verification service.

 

We're not sitting in front of your screen, so it's your job to provide relevant information. We cannot debug your code based on “It doesn't work”.

 

In any case, copying and pasting random code from the Internet is definitely not a good idea. Most of it is outdated and/or poorly written. Either read the official documentation and write your own code. Or use a well-known library (I'm not sure if there is one for the new reCAPTCHA).

Link to comment
https://forums.phpfreaks.com/topic/303149-google-captcha/#findComment-1542537
Share on other sites

Hey Jacques1,

 

I didn't really know much about cURL, so I had to read up and learn how it works.  I used a var_dump to help figure out what I was doing as well.  Bottom line is, you got me pointed in the right direction so thank you for that.

 

Here is the code that works:

$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => [
        'secret' => '***SECRET KEY HERE***',
        'response' => $_POST['g-recaptcha-response'],
    ],
]);

$response = json_decode(curl_exec($curl));

if($response->success){
    echo "<p>Captcha was successful!</p>";
}

var_dump($response);

One curious question... is that my editor phpDesigner 8 gives me an error on the "curl_setopt_array" line with syntax error expected '['

 

Yet the code runs great.  Is there something I am missing here?

Link to comment
https://forums.phpfreaks.com/topic/303149-google-captcha/#findComment-1542539
Share on other sites

One curious question... is that my editor phpDesigner 8 gives me an error on the "curl_setopt_array" line with syntax error expected '['

 

If the editor is very old (pre PHP 5.4), then it might not recognize the short array syntax. Get an update or switch to a proper IDE like Netbeans or Eclipse.

Link to comment
https://forums.phpfreaks.com/topic/303149-google-captcha/#findComment-1542540
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.