Jump to content

Help needed installing Google Recaptcha onto my basic php form


Andrew7v

Recommended Posts

Hi,

I have a basic php form that has some if(empty) code that check to make sure the fields are filled out and then if ok, it processes and emails me the data. It's all on the same php page.

But I'm having a hard time trying to figure out how to get Google Recaptcha to work...  

Can someone please help me figure this out? Thanks

 

*Attached is my form with all the important parts, and asterisks where my google captcha site keys would be...

contactform.php

Link to comment
Share on other sites

Read the forum rules.

  • When you expect help with a problem, you need to provide concrete information: symptoms, error messages, your attempts at solving them etc. Simply posting a script and telling us that there's something wrong with it doesn't work. We cannot read your mind, we're not sitting in front of your screen, and we aren't going to play guess-what-my-problem-is with you.
  • When you post code, use code tags. Attaching it as a file is very annoying, because it cannot be accesses by guests, it cannot be searched, it cannot be indexed by Google, and it doesn't have syntax highlighting.
Edited by Jacques1
Link to comment
Share on other sites

I'll help you with what I thought was the tricky part of Google ReCaptcha (How I resolved it was not only going to Google, but searching the internet. You'll be surprised how many before you have had the same issue), for I think you can get the rest by going to Google themselves.

 

When the user clicks on the I'm human checkbox this is how I would set up the response.

if (isset($submit) && $submit === 'submit') {
    /* The Following to get response back from Google Recaptha */
    $url = "https://www.google.com/recaptcha/api/siteverify";

    $remoteServer = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_URL);
    $response = file_get_contents($url . "?secret=" . PRIVATE_KEY . "&response=" . \htmlspecialchars($_POST['g-recaptcha-response']) . "&remoteip=" . $remoteServer);
    $recaptcha_data = json_decode($response);
    /* The actual check of the recaptcha */
    if (isset($recaptcha_data->success) && $recaptcha_data->success === TRUE) {
        /* Example data gather from the form (actually my contact form) */
        $data['name'] = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        $data['email'] = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        $data['phone'] = filter_input(INPUT_POST, 'phone', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        $data['website'] = filter_input(INPUT_POST, 'website', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        $data['reason'] = filter_input(INPUT_POST, 'reason', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        $data['comments'] = filter_input(INPUT_POST, 'comments', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        /* The following would be your way of sending the email */
        $send = new Email($data); // This is my way, by sending it to a class:
    } else {
        $errMessage = "You're not a human!";
    }
}
Edited by Gandalf64
  • Like 2
Link to comment
Share on other sites

The code above has its own problems (like the reliance on the potentially insecure allow_url_fopen setting and the random and incorrect use of HTML-escaping).

 

So instead of continuously relying on others to spoonfeed you (half-)working code, you should take the chance to grow up as a programmer and learn how to solve problems yourself. Yes, that's more work than the usual “My code is broken, please fix it for me!”, but it's worth it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.