0
votes

I have tried deleting and getting new site keys on a previously working google recaptcha form I used on another website.

Whatever I do (and i have moved the script in to the head etc...) wont make the recaptcha work.

There are 2 files (form.php) is a template file with html called in in wordpress and (form_process.php) is the file that works behind the scenes to submit the form. I am completely lost as to how to get the error code of 'error for site owner: invalid site key' to stop appearing and the captcha work

Thanks in advance

Form.php >

<article id='contact-page'>    
<section class='section-class' data-section-name='Contact'>
    <?php include ('form_process.php');?>        
    <form id="contact" method="post" >
        <div id="column-flex-left-301">
            <div class="image-spacer"></div>
                <h1 class="">Contact</h1>
                <fieldset id="field-no-ui">
                    <input placeholder="Your name" type="text" tabindex="1" name="name1" value="<?= $name ?>" >
                </fieldset>
                <span class="error"><?= $name_error ?></span>
                <fieldset id="field-no-ui">
                <input placeholder="Your Email Address" type="text" name="email" value="<?= $email ?>" tabindex="2" >
                </fieldset>                <div class="image-spacer"></div>
                <span class="error"><?= $email_error ?></span>
        </div>
        <div id="column-flex-right-301">
                <fieldset id="field-no-ui">
                    <textarea id="field-no-ui" class="msg-area" placeholder="Type your Message Here...." name="message" value="<?= $message ?>" tabindex="3" ></textarea>
                </fieldset>
                <div class="g-recaptcha" data-sitekey="xxx"></div>
                <span class="captcha-failed"><?= $captchafailed; ?></span>
                <span class="sent"><?= $sent; ?></span>
                <fieldset id="field-no-ui-submit">
                <button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
                </fieldset>
            <script src="https://www.google.com/recaptcha/api.js" async defer></script>
        </div>
    </form>
</section>
</article>

and form_process.php >

<?php
function post_captcha($user_response) {
$fields_string = '';
$fields = array(
    'secret' => 'xxx',
    '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);
}
$res = post_captcha($_POST['g-recaptcha-response']);
$name_error = $email_error = $captchafailed = "";
$name = $email = $message = $sent = "";
if (isset($_POST['submit']) AND (!$res['success'])) {    
    $captchafailed = "please check reCaptcha";
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name1"])) {
    $name_error = "Name is required";
} else {
    $name = test_input($_POST["name1"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
        $name_error = "Only letters and white space allowed";
    }
}
if (empty($_POST["email"])) {
    $email_error = "Email is required";
} else {
    $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $email_error = "Invalid email format";
    }
}
if (empty($_POST["message"])) {
    $message = "";
} else {
    $message = test_input($_POST["message"]);
}
if ($name_error == '' and $email_error == '' and ($res['success']) ){
    $message_body = '';
    unset($_POST['submit']);
    foreach ($_POST as $key => $value){
        $message_body .=  "$key: $value\n";
    }
    $email = $_POST['email'];
    $to = 'xxx';
    $subject = 'Contact Form Submit';
    $headers = 'From:' . $email . "\n" . 'Reply-to: ' . $email . "\n"  ;
    if (mail($to, $subject, $message, $headers)) {
        $sent = "Message sent";
        $name = $email = $message = '';
    }
}    
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
1

1 Answers

0
votes

Problem solved, I had written the html for the recaptcha on my front-page.php and wasn't calling in the old form.php template so the site key was different in the html