-1

I have a form that is posting data to a PHP page on the same local site. I know that data is posting correctly because var_dump($_POST) outputs an array with the expected information, as shown below.

However, when I try to set a variable to one of the values in the array, I get an error notice in the logs saying that I have an undefined index (for the email and g-captcha response field) and the PHP code does not work as expected.

I have been banging my head to try and figure out why this is happening for quite some time and other similar questions haven't revealed an answer. Any help would be appreciated. Thanks!

Form code:

<form action="recaptcha.php" method="post" class="pardot-email-form-handler" id="carter" novalidate>
    <input class="one-field-pardot-form-handler" maxlength="80" name="email" id="email" size="20" type="email" placeholder="Enter Email Address" required="required" />
    <div style="position:absolute; left:-9999px; top: -9999px;">
        <label for="pardot_extra_field">
            Comments
        </label>
        <input type="text" id="pardot_extra_field" name="pardot_extra_field">
    </div>
    <button class="g-recaptcha" data-sitekey="6LeErDUUAAAAAEOYqDrylFnEwQkYp7qmPjs2_Z0o" data-callback="captchaSubmit" data-badge="inline">
        Submit
    </button>
</form>

PHP Code:

var_dump($_POST);
// reCaptcha info
$secret = "anonymous";
$remoteip = $_SERVER["REMOTE_ADDR"];
$url = "https://www.google.com/recaptcha/api/siteverify";

// Form info
$email = $_POST["email"];
$response = $_POST["g-recaptcha-response"];

// Curl Request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
    'secret' => $secret,
    'response' => $response,
    'remoteip' => $remoteip
    ));
$curlData = curl_exec($curl);
curl_close($curl);

// Parse data
$recaptcha = json_decode($curlData, true);
if ($recaptcha["success"])
    echo "Success!";
else
    echo "Failure!";

Var_Dump results:

  array (size=3)
  'email' => string 'an email address' (length=21)
  'pardot_extra_field' => string 'a comment' (length=12)
  'g-recaptcha-response' => string 'a long response' (length=441)

PHP error message:

[01-Jan-2018 15:42:02 UTC] PHP Notice: Undefined index: first in C:\wamp\www\local\public\recaptcha.php on line 17

[01-Jan-2018 15:42:02 UTC] PHP Stack trace:

[01-Jan-2018 15:42:02 UTC] PHP 1. {main}() C:\wamp\www\local\public\recaptcha.php:0

[01-Jan-2018 15:42:02 UTC] PHP Notice: Undefined index: last in C:\wamp\www\local\public\recaptcha.php on line 18

[01-Jan-2018 15:42:02 UTC] PHP Stack trace:

[01-Jan-2018 15:42:02 UTC] PHP 1. {main}() C:\wamp\www\local\public\recaptcha.php:0

RamC
  • 1,287
  • 1
  • 11
  • 15
dpayne
  • 273
  • 3
  • 14
  • Most likely you are looking at two different calls to that script. One using http post and delivering the expected dump and another, unexpected http get call that results in the warning entries in your log file. – arkascha Jan 01 '18 at 17:30
  • include yout error message aswell in the question – RamC Jan 01 '18 at 17:31
  • @RamC Thanks for the reminder! Error message added. – dpayne Jan 01 '18 at 17:34
  • your array does not have the element `first`. Hence the error – RamC Jan 01 '18 at 17:35
  • @arkascha Hmm...maybe I'm misunderstanding but how would that be the case? The error message are related to attempting to set these variables, which use _POST: $email = $_POST["email"]; $response = $_POST["g-recaptcha-response"]; – dpayne Jan 01 '18 at 17:36
  • BTW, are you sure that what you have shared is the `recaptcha.php` file ? – RamC Jan 01 '18 at 17:38
  • @RamC Yes. Also, if it wasn't clear that this question was a noob question, I'll follow-up with another one - how do I add the element first to the array? – dpayne Jan 01 '18 at 17:41
  • If it was, where are you referring the element first in the code which you have provided ? – RamC Jan 01 '18 at 17:44
  • @RamC Sorry but not sure I understand what you're asking...are you asking from where the form posted? – dpayne Jan 01 '18 at 17:52
  • Your error message says `Undefined index first` meaning, you are looking for the element `first` inside an array which does not have it. Now my question is, In which line of your code are you looking for the element `first`? – RamC Jan 01 '18 at 17:54
  • @RamC Ah, I see. Thanks for the clarification! It's here: // Form info $email = $_POST["email"]; $response = $_POST["g-recaptcha-response"]; – dpayne Jan 01 '18 at 18:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162293/discussion-between-ramc-and-dpayne). – RamC Jan 01 '18 at 18:01

2 Answers2

2

From the error message that you have added, the code is looking for the index first/last in an array and unable to find it.

But there does not seem to be any such reference of these key names in your code.

There does not seem to be an array with indexes first or last in your code.

And also there are no form input tags named first or last.

Check if you are referring the proper file.

RamC
  • 1,287
  • 1
  • 11
  • 15
  • 1
    So the issue was that when I was first setting this up, I had originally added a dummy form with first and last name fields and set variables in the php field to point at those. I subsequently updated them but must not have saved the php file or something because it was still using those variables. I was confused because I thought they referred to the first and last items in the array, and not to parts of a string but @RamC helped to show me what was going on. – dpayne Jan 01 '18 at 18:36
0

here(windows 10,xampp,PHP Version 7.1.8) with "small" modifies i got your secret code is wrong(invalid-input-secret see google recaptcha returns false due to "invalid-input-secret") but how you see the result exist after you validate captcha https://developers.google.com/recaptcha/docs/display

Result: array(3) { ["email"]=> string(16) "sadasd@sdasd.com" ["pardot_extra_field"]=> string(0) "" ["g-recaptcha-response"]=> string(334) "03AMPJSYXAiSFTZyU0mzcYHYpzpKF8EODvfGkWJHQjc8fgNVNFgv3lm7ZSZVwSpwXtvHRS3JnNwAtsnfsdwpQMXz10zw7xSmhx8Jd7IkZ6J86Boh70WXXBjrczIGMcndbHZSW3boZucbO8sneKrcuZDsIqrGsmi00ECwDZ3ntvpq5qATQeDN5nhuSOesdgdG7V_XMjKINHPUZPh0ZA6fPsMXvD4GNeGqOvaB1OJidQvaxcdK7vIr-aOqr5tOJNbwxSRR941caXJT5ggkWnA4O2bzQrphcIdQ3USyO0OcKPNBqN1IWGE18XGE0ioesL3YScvoMKFzWkeF-F" } 03AMPJSYXAiSFTZyU0mzcYHYpzpKF8EODvfGkWJHQjc8fgNVNFgv3lm7ZSZVwSpwXtvHRS3JnNwAtsnfsdwpQMXz10zw7xSmhx8Jd7IkZ6J86Boh70WXXBjrczIGMcndbHZSW3boZucbO8sneKrcuZDsIqrGsmi00ECwDZ3ntvpq5qATQeDN5nhuSOesdgdG7V_XMjKINHPUZPh0ZA6fPsMXvD4GNeGqOvaB1OJidQvaxcdK7vIr-aOqr5tOJNbwxSRR941caXJT5ggkWnA4O2bzQrphcIdQ3USyO0OcKPNBqN1IWGE18XGE0ioesL3YScvoMKFzWkeF-F{ "success": false, "error-codes": [ "invalid-input-secret" ] }Failure!

<?php
var_dump($_POST);
    // reCaptcha info
    $secret = "anonymous";
    $remoteip = $_SERVER["REMOTE_ADDR"];
    $url = "https://www.google.com/recaptcha/api/siteverify";


if(isset($_POST["email"])){

    // Form info
    $email = $_POST["email"];
    $response = $_POST["g-recaptcha-response"];

    // Curl Request
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, array(
        'secret' => '6LeErDUUAAAAAEOYqDrylFnEwQkYp7qmPjs2_Z0o',//$secret,
        'response' => $response,
        'remoteip' => $remoteip
        ));
    $curlData = curl_exec($curl);
    curl_close($curl);
print_r($response);
print_r($curlData);
    // Parse data
    $recaptcha = json_decode($curlData, true);
    if ($recaptcha["success"])
        echo "Success!";
    else
        echo "Failure!";
}
?>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>

<!-- recaptcha.php --><form action="" method="post" class="pardot-email-form-handler" id="carter" novalidate>
    <input class="one-field-pardot-form-handler" maxlength="80" name="email" id="email" size="20" type="email" placeholder="Enter Email Address" required="required" />
    <div style="position:absolute; left:-9999px; top: -9999px;">
        <label for="pardot_extra_field">
            Comments
        </label>
        <input type="text" id="pardot_extra_field" name="pardot_extra_field">
    </div>
    <!-- <button class="g-recaptcha" data-sitekey="6LeErDUUAAAAAEOYqDrylFnEwQkYp7qmPjs2_Z0o" data-callback="captchaSubmit" data-badge="inline">
        Submit
    </button> -->
    <input type="submit" value="Submit">

     <div class="g-recaptcha" data-sitekey="6LeErDUUAAAAAEOYqDrylFnEwQkYp7qmPjs2_Z0o"></div>

</form><hr><?php phpinfo(); ?>
  • I added an anonymous secret code so that people wouldn't be able to see it publicly. A secret code that was not specifically applied to my domains will not work and it looks like you used my public key instead, which also will not work. – dpayne Jan 01 '18 at 18:39
  • i agree.. the new script with your secret key works on that specific domain? 'cause sending requests from 127.0.0.1 isn't a good ideea –  Jan 01 '18 at 18:42