Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当var\u dump($\u POST)显示数组时,为什么会出现未定义的索引错误?_Php_Arrays_Post_Var Dump - Fatal编程技术网

Php 当var\u dump($\u POST)显示数组时,为什么会出现未定义的索引错误?

Php 当var\u dump($\u POST)显示数组时,为什么会出现未定义的索引错误?,php,arrays,post,var-dump,Php,Arrays,Post,Var Dump,我有一个表单,将数据发布到同一本地站点的PHP页面。我知道数据发布是正确的,因为var\u dump($\u POST)输出一个包含预期信息的数组,如下所示 但是,当我尝试将一个变量设置为数组中的一个值时,我在日志中收到一个错误通知,说我有一个未定义的索引(用于电子邮件和g-captcha响应字段),PHP代码无法按预期工作 我一直在绞尽脑汁想弄明白为什么会发生这种情况已经有一段时间了,而其他类似的问题还没有找到答案。任何帮助都将不胜感激。谢谢 表格代码: <form action="re

我有一个表单,将数据发布到同一本地站点的PHP页面。我知道数据发布是正确的,因为
var\u dump($\u POST)
输出一个包含预期信息的数组,如下所示

但是,当我尝试将一个变量设置为数组中的一个值时,我在日志中收到一个错误通知,说我有一个未定义的索引(用于电子邮件和g-captcha响应字段),PHP代码无法按预期工作

我一直在绞尽脑汁想弄明白为什么会发生这种情况已经有一段时间了,而其他类似的问题还没有找到答案。任何帮助都将不胜感激。谢谢

表格代码:

<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>
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!";
  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)
变量转储结果:

<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>
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!";
  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错误消息:

<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>
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!";
  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)
[01-Jan-2018 15:42:02 UTC]PHP通知:未定义索引:第一 C:\wamp\www\local\public\recaptcha.php,第17行

[01-Jan-2018 15:42:02 UTC]PHP堆栈跟踪:

[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通知:未定义索引:最后一个 C:\wamp\www\local\public\recaptcha.php,第18行

[01-Jan-2018 15:42:02 UTC]PHP堆栈跟踪:

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


从您添加的错误消息中,代码正在数组中查找索引
first
/
last
,但找不到它

但是,在代码中似乎没有对这些键名称的引用

在您的代码中似乎没有一个首先或最后带有索引的数组

而且也没有名为first或last的表单输入标记

检查您是否引用了正确的文件。

此处(windows 10,xampp,PHP 7.1.8版)带有“小”修改。我得到的您的密码是错误的(无效输入密码请参见),但验证验证码后您如何看到结果

结果: 数组(3){[“电子邮件”]=>字符串(16)”sadasd@sdasd.com“[“pardot_额外_字段”]=>string(0)”“[“g-recaptcha-response”]=>string(334)3.作为一个政府部门的一部分,一个政府部门的一个政府部门的一个政府部门的一个政府部门的一个政府部门的一个政府部门的一个政府部门的一个非政府部门的一个非政府部门的一个非政府部门的一个非政府部门的一个非政府部门的一个非政府部门的一个非政府部门的非政府部门的非政府部门的非政府的非政府部门的非政府的非政府部门的非政府的非政府部门的非政府的非政府的非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府非政府3.在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国,在中国:false,“错误代码”:[“无效输入密码”]}失败

<?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(); ?>

评论


您很可能看到对该脚本的两个不同调用。一个使用http post并传递预期的转储,另一个意外的http get调用导致日志文件中出现警告条目。在question@RamC谢谢你的提醒!添加了错误消息。您的数组没有元素
first
。因此error@arkascha嗯……也许我误解了,但怎么会是这样呢?错误消息与试图设置这些变量有关,这些变量使用_POST:$email=$_POST[“email”]$响应=$_POST[“g-recaptcha-response”];所以问题是,当我第一次设置它时,我最初添加了一个带有名字和姓氏字段的虚拟表单,并在php字段中设置变量以指向这些字段。我随后更新了它们,但肯定没有保存php文件或其他东西,因为它仍在使用这些变量。我很困惑,因为我认为他们引用了数组中的第一个和最后一个项,而不是字符串的某些部分,但@RamC帮助我了解了发生了什么。我添加了一个匿名密码,这样人们就不能公开看到它。未专门应用于我的域的密码将不起作用,而且您似乎使用了我的公钥,这也将不起作用。我同意。。带有您的密钥的新脚本可在该特定域上工作?“因为从127.0.0.1发送请求不是一个好主意