PHP header() 使用 POST 变量重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4281900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:31:16  来源:igfitidea点击:

PHP header() redirect with POST variables

phphtmlpostheader

提问by Yoshiyahu

I'm working with PHP, and I'm making an action page which a form posts to. The page checks for errors, then if everything is fine, it redirects them to the page where the data has been posted. If not, I need to to redirect them back to the page they were at with an error and the POST variables. Here is the gist of how it works.

我正在使用 PHP,并且正在制作一个表单发布到的操作页面。该页面检查错误,然后如果一切正常,它会将它们重定向到已发布数据的页面。如果没有,我需要将它们重定向回它们所在的页面,并显示错误和 POST 变量。这是它如何工作的要点。

The HTML would look like this...

HTML看起来像这样......

<form name="example" action="action.php" method="POST">
  <input type="text" name="one">
  <input type="text" name="two">
  <input type="text" name="three">
  <input type="submit" value="Submit!">
</form>

action.php would look like this...

action.php 看起来像这样...

if(error_check($_POST['one']) == true){
    header('Location: form.php');
    // Here is where I need the data to POST back to the form page.
} else {
    // function to insert data into database
    header('Location: posted.php');
}

In the case of an error, I need it to POST back to the first page. I can't use GET, because the input will be too large. I don't want to use SESSION, if possible. Is this possible?

如果出现错误,我需要将其 POST 回第一页。我不能使用 GET,因为输入会太大。如果可能,我不想使用 SESSION。这可能吗?

采纳答案by deceze

If you don't want to use sessions, the only thing you can do is POST to the same page. Which IMO is the best solution anyway.

如果您不想使用会话,您唯一可以做的就是 POST 到同一页面。无论如何,哪个 IMO 是最好的解决方案。

// form.php

<?php

    if (!empty($_POST['submit'])) {
        // validate

        if ($allGood) {
            // put data into database or whatever needs to be done

            header('Location: nextpage.php');
            exit;
        }
    }

?>

<form action="form.php">
    <input name="foo" value="<?php if (!empty($_POST['foo'])) echo htmlentities($_POST['foo']); ?>">
    ...
</form>

This can be made more elegant, but you get the idea...

这可以做得更优雅,但你明白了......

回答by bowsersenior

// from http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl
function do_post_request($url, $data, $optional_headers = null)
{
  $params = array('http' => array(
              'method' => 'POST',
              'content' => $data
            ));
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}

回答by Jan Thom?

It is not possible to redirect a POST somewhere else. When you have POSTED the request, the browser will get a response from the server and then the POST is done. Everything after that is a new request. When you specify a location header in there the browser will always use the GET method to fetch the next page.

无法将 POST 重定向到其他地方。当您发布请求时,浏览器将从服务器获得响应,然后完成 POST。之后的一切都是一个新的请求。当您在其中指定位置标头时,浏览器将始终使用 GET 方法来获取下一页。

You could use some Ajax to submit the form in background. That way your form values stay intact. If the server accepts, you can still redirect to some other page. If the server does not accept, then you can display an error message, let the user correct the input and send it again.

您可以使用一些 Ajax 在后台提交表单。这样您的表单值保持不变。如果服务器接受,您仍然可以重定向到其他页面。如果服务器不接受,那么您可以显示错误消息,让用户更正输入并再次发送。

回答by Evan Mulawski

It would be beneficial to verify the form's data before sending it via POST. You should create a JavaScript function to check the form for errors and then send the form. This would prevent the data from being sent over and over again, possibly slowing the browser and using transfer volume on the server.

在通过 POST 发送之前验证表单的数据将是有益的。您应该创建一个 JavaScript 函数来检查表单是否有错误,然后发送表单。这将防止数据被一遍又一遍地发送,可能会减慢浏览器的速度并使用服务器上的传输量。

Edit:

编辑:

If security is a concern, performing an AJAX request to verify the data would be the best way. The response from the AJAX request would determine whether the form should be submitted.

如果考虑安全性,执行 AJAX 请求来验证数据将是最好的方法。AJAX 请求的响应将确定是否应提交表单。

回答by Derrick

Use a smarty template for your stuff then just set the POST array as a smarty array and open the template. In the template just echo out the array so if it passes:

为您的东西使用 smarty 模板,然后只需将 POST 数组设置为 smarty 数组并打开模板。在模板中只需回显数组,如果它通过:

if(correct){
    header("Location: passed.php");
} else {
    $smarty->assign("variables", $_POST);
    $smarty->display("register_error.php");
    exit;
}

I have not tried this yet but I am going to try it as a solution and will let you know what I find. But of course this method assumes that you are using smarty.

我还没有尝试过这个,但我将尝试将其作为解决方案,并将让您知道我的发现。但是当然这种方法假设您使用的是smarty。

If not you can just recreate your form there on the error page and echo info into the form or you could send back non important data in a get from and get it

如果不是,您可以在错误页面上重新创建表单并将信息回显到表单中,或者您可以在 get from 中发回非重要数据并获取它

ex.

前任。

register.php?name=mr_jones&address==......
echo $_GET[name];