Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.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 在同一页上显示recaptcha错误消息_Php_Recaptcha - Fatal编程技术网

Php 在同一页上显示recaptcha错误消息

Php 在同一页上显示recaptcha错误消息,php,recaptcha,Php,Recaptcha,我有两页 第1页有一个表单,可以将值发布到第2页 第2页有以下内容: if (!$resp->is_valid) { // What happens when the CAPTCHA was entered incorrectly die ("The reCAPTCHA wasn't entered correctly. Go back and try it again. " . "(reCAPTCHA said: " . $resp->error

我有两页

第1页有一个表单,可以将值发布到第2页

第2页有以下内容:

if (!$resp->is_valid) {

    // What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again. " .
         "(reCAPTCHA said: " . $resp->error . ")");

} else {

如何在第1页的recaptcha控件下显示该值或更合适的值,而不是在第2页显示
die
输出?

您可以在第1页的
if(isset($\u POST['submit')){…}
块中进行设置。在此块中,您可以检查recaptcha和
echo“”
错误消息。

您应该将请求发布到第1页,并在那里添加recaptcha代码。然后,如果出现错误,您可以设置名为
$error
的变量,而不是调用
die(…);
。然后,在recaptcha表单之后,您可以检查是否设置了
$error
,然后显示错误消息。这将类似于以下snipet:

if( isset( $error ) ) {
    echo '<p class="error">' . $error . '</p>';
}
if(设置($error)){
回显“

”.$error.

”; }

我希望这能有所帮助。

您可以将表格提交到第1页,并检查验证码是否正确:

  • 如果不正确,您可以在表单本身附近的某个位置显示消息
  • 如果正确,您可以设置cookie/设置一些会话变量并重定向到第2页

另一种方法是,在第2页上,如果验证码不正确,而不是将
die
重定向回第1页(并且您也可以设置一些会话变量,以便在重定向后在第1页上显示错误)

在一个脚本中获取错误状态,但希望在另一个脚本中使用该值。它有下一个解决方案: 1) 使用$\u会话数组保存验证状态 在脚本2中:

if (!$resp->is_valid) {

    // What happens when the CAPTCHA was entered incorrectly
    $_SESSION['error_status'] = $resp->error;

} else {
在脚本1中:

if ($_SESSION['error_status']){
    echo "<p class='error'>{$_SESSION['error_status']}</p>"
}
if($\u会话['error\u status'])){
echo“

{$\u会话['error\u status']}

” }
2) 或者您可以在标题中添加错误状态('Location:'。$error\u code); 在第二个脚本中,我有这个