0

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 } 
1
  • 1
    var_dump($response); That's the javascript way of getting array/object values; in PHP, the period is for concatenation. If $response is an array, it would be $response['success'], if it's an object, it's $response->success. Commented Jan 17, 2017 at 15:04

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.