integrating Google reCaptcha into mail.php

the mail() function

Moderators: egami, macek, gesf

Post Reply
creaitken
New php-forum User
New php-forum User
Posts: 1
Joined: Wed Jun 21, 2017 6:40 am

Wed Jun 21, 2017 11:33 am

Hi guys

Sorry for this but I am stuck. I am lost with regards to php and although I can implement the html side of things, I dont have a clue where to place the recaptcha scripting in the php. Can anyone help me? Happy to pay for assistance. Cheers, Glenn

Its this part:

When your users submit the form where you integrated reCAPTCHA, you'll get as part of the payload a string with the name "g-recaptcha-response". In order to check whether Google has verified that user, send a POST request with these parameters:
URL: https://www.google.com/recaptcha/api/siteverify
secret (required) 6LetXCYUAAAAACAn2FncL1ycHTEwwhA729jerj4N
response (required) The value of 'g-recaptcha-response'.
remoteip The end user's ip address.

User avatar
GeorgeLoo
New php-forum User
New php-forum User
Posts: 2
Joined: Tue Aug 01, 2017 4:19 am

Tue Aug 01, 2017 4:23 am

I recently started programming and recently faced the same problem on my site. thanks for the answer

User avatar
Strider64
php-forum Active User
php-forum Active User
Posts: 315
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI
Contact:

Tue Aug 01, 2017 8:39 am

Here's how I do it ->

Code: Select all

$submit = filter_input(INPUT_POST, 'submit', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
if (isset($submit) && $submit === 'submit') {
    /* The Following to get response back from Google Recaptcha */
    $url = "https://www.google.com/recaptcha/api/siteverify";
    /* Remote Server  */
    $remoteServer = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_URL);
    /* The response you get back from Google verifying the user is indeed a human */
    $response = file_get_contents($url . "?secret=" . PRIVATE_KEY . "&response=" . \htmlspecialchars($_POST['g-recaptcha-response']) . "&remoteip=" . $remoteServer);
    /* Put it in a form that PHP can understand */
    $recaptcha_data = json_decode($response);
    /* The actual check of the recaptcha */
    if (isset($recaptcha_data->success) && $recaptcha_data->success === TRUE) { // Success 
        $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);

        $send = new Email($data);
    } else {
        $success = "You're not a human!"; // Person is not a HUMAN...
    }
}
Life is a fig newton of your imagination! https://www.pepster.com/index

Post Reply