HenryM
HenryM

Reputation: 5793

reCAPTCHA always coming across as true

What is wrong with this bit of code. Whether I tick the reCAPTCHA or not, it goes onto the else clause.

<?php
// This is added for Google Captcha

    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $privatekey = '6LdBjyATAAAAABZe1O-DKBEQnOIzanoVLGEvsvyu';

    $response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
    $data = json_decode($response);

    if($response.success==false){
        echo "<h2>Spam Spam go away</h2><p>And if you're not spam, we apologise.  Please go back and tick the reCAPTCHA box.</p><p>Thank you</p>";
        die();
    } else {
// do loads of clever stuff
}

Upvotes: 0

Views: 575

Answers (1)

Blaatpraat
Blaatpraat

Reputation: 2849

In PHP the dot . operator is for appending strings.
Because the weakly typing of PHP you can append strings to everything.

This line of code:

if($response.success==false){

Would append 'success' to the $response stdClass.
If you enable notices, this would trigger a notice, because of that the string is not inside quotes.

The output string is in that case success, and that is not false in PHP.

What you want, is this:

if($response->success==false){

You need to access it as a property of stdClass.

Upvotes: 2

Related Questions