1
votes

I'm attempting to create a reCAPTCHA human verification box on my website, however I cannot get past an error in my php code. I'm sure it's simple, I just can't get past it. I took out my site and private keys for security reasons.

My error when compiling:

"FATAL ERROR syntax error, unexpected ';' on line number 8"

Here's my code, thanks in advance:

    <?php

    if(isset($_POST['ContactButton'])){
        $url = 'https://www.google.com/recaptcha/api/siteverify';
        $privatekey = "--- My Private Key ---";
        $response = file_get_contents($url."?secret=".$#privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);

        $data = json_decode($response);

        if(isset($data->success) AND $data->success==true){
            ////true - what happens when user is verified
            header('Location: ExampleCaptcha.php?CaptchaPass=True');
        }else{
            ////false - what happens when the user ISNT verified
            header('Location: ExampleCaptcha.php?CaptchaFail=True');
        }
    }

?>




<html>
    <head>
        <title>index</title>
        <script src='https://www.google.com/recaptcha/api.js'></script>



    </head>
    <body>
        <form method='post' action='index.html'>
            <input type='text' name='inp' /><br>
            <div class="g-recaptcha" data-sitekey="--- My Site Key ---"></div><br>
            <input type='submit' /><br>
        </form>
    </body>
</html>
1
which line is line 8?Martin
rather than if(isset($data->success) AND $data->success==true){ try if( property_exists( $data, 'success' ) && $data->success==true ){Professor Abronsius
@RamRaider there are various improvements OP can make to his code, but this doesn't relate to their current issue.Martin
@Martin Line 8 is... $data = json_decode($response);Isaac Worley
see my answer, @IsaacWorley I'm pretty sure the issue is in your file_get_contents stringMartin

1 Answers

1
votes

your error is here: file_get_contents($url."?secret=".$#privatekey. there is a hash (comment) symbol in the string which looks like it is in the wrong place, (typo?)... but your string looks a mess overall. Improve this. The Error will resolve itself.

Remove the # you have incorrectly placed. variables can not start with a symbol.

...

$string = $url."?secret=".$#privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR'];
                         ^^^^ //What's this? Remove '#' 

$response = file_get_contents($string);