0

Right, I am really beginning to lose my tempter with this thing. Why, whenever I use the code that Google Publish to enable their Google Captcha document do I just get the raw code showing on the webpage, despite converting from an HTML to a PHP document and following everything to the letter?

This was an HTML document, and the file has been changed to the php extension, there is still HTML code in the document but php code in nestled in the body tags.

<!doctype html>
<html>
<head>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<!---HTML CODE HERE--->
<?php
// Checks if form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    function post_captcha($user_response) {
        $fields_string = '';
        $fields = array(
            'secret' => '_______________PRIVATE_KEY_______________',
            'response' => $user_response
        );
        foreach($fields as $key=>$value)
        $fields_string .= $key . '=' . $value . '&';
        $fields_string = rtrim($fields_string, '&');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

        $result = curl_exec($ch);
        curl_close($ch);

        return json_decode($result, true);
    }

    // Call the function post_captcha
    $res = post_captcha($_POST['g-recaptcha-response']);

    if (!$res['success']) {
        // What happens when the CAPTCHA wasn't checked
        echo '<p>Please go back and make sure you check the security CAPTCHA box.</p><br>';
    } else {
        // If CAPTCHA is successfully completed...

        // Paste mail function or whatever else you want to happen here!
        echo '<br><p>CAPTCHA was completed successfully!</p><br>';
    }
} else { ?>
    
<!-- FORM GOES HERE -->
<form method="POST" action="/scripts/mail_html.php" enctype="multipart/form-data">
                <input type="email" name="email" required placeholder="Enter your email"/>
        <div class="g-recaptcha" data-sitekey="_______________PUBLIC_KEY_______________"></div><br/><br/>
                <p>By subscribing you agree to receive marketing communications from xxxxxl. You can unsubscribe anytime using the link in the footer of any of our emails. See our <a style="text-decoration: underline;" class="inline_link__new_window" target="_new" href="xxxx">Privacy Policy</a></p><br/>
                <input type="submit" value="Subscribe"/>
</form>

<?php 
} ?>

</body>
</html>

As you can see, you can clearly see the coding as opposed to just the form.

Why?

  • `?php` needs to be ` – ADyson Jun 10 '21 at 07:07
  • @ADyson, thanks, this has been corrected, but it's still showing the raw code. – thelovablecodeguy Jun 10 '21 at 07:10
  • Does this answer your question? [PHP code is not being executed code shows in browser](https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-code-shows-in-browser) – ADyson Jun 10 '21 at 07:11
  • No, question edited. – thelovablecodeguy Jun 10 '21 at 07:19
  • Have you done _all_ the things in the accepted answer on that link? Like install PHP and run the code using localhost? In 99% of cases the cause of this issue is one (or more) of the things on that list. – ADyson Jun 10 '21 at 07:25
  • Can you run a simple PHP hello world program? – ADyson Jun 10 '21 at 07:27
  • How exactly did you set your PHP up? What o/s are you using? Normally you can just install a package like xampp and be up and running in a few minutes, load up `http://localhost/index.php` and off you go, no faffing required. P.s. there's no need to rant and rave, just stick to providing information we could use to help you – ADyson Jun 10 '21 at 08:22
  • I appreciate you will be frustrated, but try to stay calm otherwise you'll just end up making mistakes or overlooking things :-) – ADyson Jun 10 '21 at 08:27

0 Answers0