我有一个表单将数据发布到同一本地站点上的PHP页面。我知道数据正确发布是因为var_dump($_POST)
输出了一个包含预期信息的数组,如下所示。
但是,当我尝试将变量设置为数组中的某个值时,我在日志中收到错误通知,说我有一个未定义的索引(对于email和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>
PHP代码:
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结果:
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错误消息:
[01-Jan-2018 15:42:02 UTC] PHP注意:未定义的索引:先进入 第17行的C:\ wamp \ www \ local \ public \ recaptcha.php
[01-Jan-2018 15:42:02 UTC] PHP Stack trace:
[01-Jan-2018 15:42:02 UTC] PHP 1. {main}() C:\瓦帕\ WWW \本地\公共\ recaptcha.php:0
[01-Jan-2018 15:42:02 UTC] PHP注意:未定义的索引:最后一次 第18行的C:\ wamp \ www \ local \ public \ recaptcha.php
[01-Jan-2018 15:42:02 UTC] PHP Stack trace:
[01-Jan-2018 15:42:02 UTC] PHP 1. {main}() C:\瓦帕\ WWW \本地\公共\ recaptcha.php:0
答案 0 :(得分:2)
从您添加的错误消息中,代码正在数组中查找索引first
/ last
,但无法找到它。
但是代码中似乎没有任何关于这些键名的引用。
在代码中似乎没有索引的第一个或最后一个数组。
并且没有名为first或last的输入标签。
检查您是否引用了正确的文件。
答案 1 :(得分:0)
这里(windows 10,xampp,PHP Version 7.1.8),&#34; small&#34;修改我的密码是错误的(无效输入密码请参阅google recaptcha returns false due to "invalid-input-secret")但是在验证验证码后如何看到结果存在 https://developers.google.com/recaptcha/docs/display
<强>结果:强> array(3){[&#34; email&#34;] =&gt; string(16)&#34; sadasd@sdasd.com" [&#34; pardot_extra_field&#34;] =&GT; string(0)&#34;&#34; [&#34; G-验证码 - 响应&#34;] =&GT;串(334)&#34; 03 AMPJSYXAiSFTZyU0mzcYHYpzpKF8EODvfGkWJHQjc8fgNVNFgv3lm7ZSZVwSpwXtvHRS3JnNwAtsnfsdwpQMXz10zw7xSmhx8Jd7IkZ6J86Boh70WXXBjrczIGMcndbHZSW3boZucbO8sneKrcuZDsIqrGsmi00ECwDZ3ntvpq5qATQeDN5nhuSOesdgdG7V_XMjKINHPUZPh0ZA6fPsMXvD4GNeGqOvaB1OJidQvaxcdK7vIr-aOqr5tOJNbwxSRR941caXJT5ggkWnA4O2bzQrphcIdQ3USyO0OcKPNBqN1IWGE18XGE0ioesL3YScvoMKFzWkeF-F" } 03 {AMPJSYXAiSFTZyU0mzcYHYpzpKF8EODvfGkWJHQjc8fgNVNFgv3lm7ZSZVwSpwXtvHRS3JnNwAtsnfsdwpQMXz10zw7xSmhx8Jd7IkZ6J86Boh70WXXBjrczIGMcndbHZSW3boZucbO8sneKrcuZDsIqrGsmi00ECwDZ3ntvpq5qATQeDN5nhuSOesdgdG7V_XMjKINHPUZPh0ZA6fPsMXvD4GNeGqOvaB1OJidQvaxcdK7vIr-aOqr5tOJNbwxSRR941caXJT5ggkWnA4O2bzQrphcIdQ3USyO0OcKPNBqN1IWGE18XGE0ioesL3YScvoMKFzWkeF-F&#34;成功&#34 ;:假,&#34;错误码&#34 ;: [&#34;无效输入秘密&#34; ]}失败!
<?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(); ?>