Use of undefined constant success

Hi,

I am using recaptcha on one of our forms, and in fairness it works fine, but am getting this error below and cant work it out.

Notice: Use of undefined constant success - assumed ‘success’ in \en\contact.php on line 34

if(isset($_POST['submit']) && !empty($_POST['submit'])){

if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){

$secret = '6Lctyjhtyjtyjty';

$postdata = http_build_query(
    array(
        'secret'    =>  $secret,
        'response'  =>  $_POST["g-recaptcha-response"]
    )
);
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context = stream_context_create($opts);

$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', FALSE, $context);

if($responseData.success != false){

I used -

$responseData = json_decode($response);
var_dump($responseData);

and got NULL, so have tried these below and then the form didnt submit successfully anymore, it possibly didnt recognise success was present.

if ($responseData["success"]==false)

if ($responseData->success==false)

Variables cannot contain periods. PHP will assume you are concatenating the vairable with a constant hence the undefined constants error. It has to either be joined with the variable or using an under score. It cannot contain hyphens as well.

Hi, are you reffering to -

$responseData.success != false

Could you show more what you mean with code please :slight_smile:

Is below correct though

$responseData = json_decode($response);

If the var_dump( $responseData ) is NULL then try this:

 if( isset( $responseData) ):
   echo 'sorry,  we have no $responseData ';
  // do stuff or exit;
endif;

Edit:
From the Free PHP Online Manual

Return Values ¶
Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

Yes. In PHP, it assumes that once you start putting in periods, it starts to assume you are concatenating the variable. So it will look for those concatenations. Since there are no dollar sign in front of success, it assumes you are referencing a constant. Thus the undefined constants error. To avoid this, you have to use a whole variable such as $responseDataSuccess.

Also, where is success coming from anyways? Are you trying to get the response of the json file? If so, this is already incorrect. json files are just like stdClass objects and as such, you must treat them like stdClass objects.

Hi John,

Just going through these messages back and wanted to double check what the var_dump was spitting out, and its actually - string(16)

So its not NULL sorry.

From

$context = stream_context_create($opts);

$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', FALSE, $context);

$responseData = json_decode($response);
var_dump($responseData." - check");

if($responseData.success != false){

So then I assume thats why isset isnt working as an option.

I have changed the code slightly to below, and the whole thing has stopped working it seems.

$context = stream_context_create($opts);

$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', FALSE, $context);

$responseData = json_decode($response, true);
//var_dump($responseData." - check");

if($responseData['success'] == true){

Sorry to keep updating, but am trying out a few options, i am this stage below and the echo I put in did show on the page, but below are a load of warnings I’m getting too.

$postdata = http_build_query(
    array(
        'secret'    =>  $secret,
        'response'  =>  $_POST["g-recaptcha-response"],
		'remoteip'  =>  $_SERVER['REMOTE_ADDR']
    )
);
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context = stream_context_create($opts);

$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);

$responseData = json_decode($response);

if(!$responseData->success){

echo ("1");

These are the warnings -

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in \\contact.php on line 33

Warning: file_get_contents(): Failed to enable crypto in \\contact.php on line 33

Warning: file_get_contents(https://www.google.com/recaptcha/api/siteverify): failed to open stream: operation failed in \\contact.php on line 33

Notice: Trying to get property of non-object in \\contact.php on line 37

I’m testing in a dev environment without the SSL certificate, but when I test it on the live server which has ssl it still doesnt work, so I think I have written that issue off

Surely those last errors are all linked, though? The SSL fails, so there are no file contents to grab, so there’s nothing in $responseData to check.

Hi droopsnoot, I haven’t a clue in honesty. So I added this code to the page

<?php 
  foreach ($_POST as $key => $value) {
    echo '<p><strong>' . $key.':</strong> '.$value.'</p>';
  }
?>

Then when I submitted after filling all the fields in, I got this below which looks fine to me.

name: uuykuykuyk

email: info@aceb.co.uk

company: jyytjtyj

phone: 07951178120

message: jytjtyjty

g-recaptcha-response: 03AJIzXZ6B(just part of a huge line of numbers and letters)

submit: Send message

So do you reckon I’m being blocked from connecting to google from my own server?

And when I var_dump $responseData I get usually something like string(6)

OK it must be a server issue, as below has a changed a little but it looks fine to me.

$secret = '6Lcz0KC';

$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array (
        'secret'    =>  $secret,
        'response'  =>  $_POST["g-recaptcha-response"]
);

$options = array('http' => array (
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);

$context = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success = json_decode($verify);

if($captcha_success->success==true){

echo ("1");

So I then changed it to

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

echo ("1");

And it echo’d out the #1, so its coming back as false for some reason

I haven’t tried it myself, it just seems logical that if the connection is refused (because the SSL isn’t working / configured) then the subsequent code that relies on stuff coming back from that call will also fail. $captcha_success->success is created when you decode the response, but you didn’t get a response, so it throws an error.

I’m surprised, though, that if the call to google is still failing with SSL issues, that it can suddenly find the success attribute to compare it to false. What's in $captcha_success` if you var_dump() it, does that give any clues, better error messages etc?

Ye this is what I thought, if it was failing then I wouldnt get false.

I found this below and changed them over and the ssl errors went away, but it still seems success isn’t being recognised.

$options = array (
		'http' => array (
			'method' => 'POST',
			'content' => http_build_query($data)
		)
);

/*$options = array (
		"ssl"=>array(
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ),
);*/ 

So changed it back and did below -
var_dump $captcha_success gives me - string(9)

Just that, or is there anything in the string? Looking at the doc for the API call, it doesn’t look as if anything should return a 9-character string. Perhaps you could var_dump($verify) instead, see what the actual response is before you send it to json_decode().

Ye same thing

$verify = file_get_contents($url, false, $context);
var_dump($verify . " - errorB");

string(9) " - errorB"

OK, so $verify is empty then, you’re not getting a response from the site.

Yep, by the looks, the code looks fine so not sure at all

Ok I might have something, i changed $options to below and got a much better var_dump

$data = array (
        'secret' => $secret,
        'response' => $_POST["g-recaptcha-response"]
);

/*$options = array (
		'http' => array (
			'method' => 'POST',
			'content' => http_build_query($data)
		)
);*/

$options = array (
        "ssl"=>array(
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ),
); 
$context = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
var_dump($verify . " - errorB");

Gave me this message -

string(112) “{ “success”: false, “error-codes”: [ “missing-input-response”, “missing-input-secret” ] } - errorB”

So it looks like I need to work out where $data goes as it was a part of ‘content’ previously, then that should do it perhaps

Does this sample code help? https://gist.github.com/jonathanstark/dfb30bdfb522318fc819

It sort of does, but they use the old way that I was using $options, which caused all the ssl errors, somehow I need to work out where the info in $data such as secret and response goes to fit in with the $options bit, as the var_dump is saying Im missing the secret code and the response $POST

OK I think I’ve got it, and will post the var_dump and the notice error I’m getting

$secret = '6LcWJ';

$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array (
	'secret' => $secret,
	'response' => $_POST["g-recaptcha-response"]
);

$options = array (
	'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false
    ),
	'http' => array (
			'method' => 'POST',
			'content' => http_build_query($data)
		)
); 

$context = stream_context_create($options);
$verify = file_get_contents($url, false, $context);

var_dump($verify . " - errorB");

$captcha_success = json_decode($verify);
if($captcha_success->success==true){

echo ("1");

Notice: file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in \\contact.php on line 64

string(114) “{ “success”: true, “challenge_ts”: “2018-04-04T12:27:53Z”, “hostname”: “dev.che.com” } - errorB”

1