We help IT Professionals succeed at work.

Why does this code generate the following php error;

dlearman1
dlearman1 asked
on
The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.


<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>reCaptcha-test2.php</title>

    
  </head>
  <body>
    <?php
      echo "reCaptcha-test.php says hello"; 
      $url = 'https://www.themagnolia.com/reCaptcha-test-2.htm';
      $inputName;$inputEmail;$inputPhone;$inputMessage;$inputAddress;$inputCity;$inputState;$inputZipcode;$captcha;
      $captcha = filter_input(INPUT_POST, 'token', FILTER_SANITIZE_STRING);
        if (!$captcha) {
          <script>consol.log ("Quit No Captcha");</script>
          header ('location: https://www.themagnolia.com/reCaptcha-test-2.htm');
          exit ('Quit... token not found');
        }

        $secretKey = 'SECRET-KEY-GOES HERE';
        //$ip = $_SERVER['REMOTE_ADDR'];

        // post request to server
        $url = 'https://www.google.com/recaptcha/api/siteverify';
        $data = array('secret' => $secretKey, 'response' => $captcha);
        
        $options = array (
          'http' => array (
            'method'  => 'POST',
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'content' => http_build_query($data),
          )
        );

      $context  = stream_context_create($options);
      $response = file_get_contents($url, false, $context);
      $responseKeys = json_decode($response,true);
      header('Content-type: application/json');

      if($responseKeys["success"] === '1' && ["action"] == $action && ["score"] >= 0.5) {
        echo "reCaptcha processing successful";
      } else {
        echo "reCaptcha processing failed";
        header ('location: https://www.themagnolia.com/reCaptcha-test.htm');
      }
    ?>
  </body>
</html>

Open in new window

Comment
Watch Question

Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Two things

1. This script should bomb on line 18
2. If it gets past line 18 it redirects to https://www.themagnolia.com/reCaptcha-test-2.htm

Where are you seeing your error?

Author

Commented:
I'm getting the error if I browse directly to https://themagnolia.com/reCaptcha-test-2.php.  There is an html page reCaptcha-test-2.htm that processes the form data and should pass the data to reCaptcha-test-2.php, except I don't think this handoff is working and the above error seems to be hiding the problem

I was hoping line 18 would be treated as javascript and send a comment to the console log.  Is there a way to write to the console.log out of php?
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
I was hoping line 18 would be treated as javascript and send a comment to the console log
You can't do this in PHP
<script>consol.log ("Quit No Captcha");</script>

Open in new window

If you want that to be written to your page then you need to echo it out
echo '<script>console.log ("Quit No Captcha");</script>';

Open in new window

[Note: you have to spell console correctly for this to work]

Is there a way to write to the console.log out of php?
PHP runs - produces output then dies.
Immediately after the funeral the output is sent to the browser and the browser processes it - so there is no direct link from PHP to the console. The only output is echo statements. Echoing out a console.log() does not really do anything unless it is in some script that activates based on a page event - otherwise you might as well output directly to the page (hidden div / debug window etc)

You can do it to the console if you don't want it on the screen - personally I prefer a hidden div for that.

However the above is moot as you will never see the console output (assuming it was correct) because your script is redirecting you to another page which will wipe the console (refer the header(location) statements)

What is it you are trying to do here?

Author

Commented:
Thanks for the guidance.  I'm just trying to implement reCaptcha v3 on an existing form.

In reCaptcha.htm, I am successful in returning the token from the Google server. I'm trying to post this token along with the form data to the reCaptcha.php. But I'm not getting any trace output, from

function (result) {
                  alert ("result has executed");
                  if (result.success) {
                    console.log("you are a success");
                  } else {
                    console.log("you are a failure");
                  }
                } 

When I try to test the code, I run into the error message.

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
I am not currently familiar with the reCaptcha process as it pertains to integrating to your page and I won't be able to look into that in detail today.

It is important to note that PHP is not producing that error - that is something the browser is throwing out because it is getting a response that it does not like

If I run your script locally - with the line 18 fixed it redirects me to https://www.themagnolia.com/reCaptcha-test-2.htm - I don't see an error.

Have you tried fixing line 18 and trying again?