使用 PHP 将 POST 数据从一个网页传递到另一个网页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/679701/
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-24 23:28:39  来源:igfitidea点击:

Passing POST data from one web page to another with PHP

phppost

提问by PTBNL

Searching did not find a similar question, so: How can POST data used by PHP to generate one page be passed from that page to another PHP generated page? I have:

搜索没有找到类似的问题,所以:PHP生成一个页面所使用的POST数据如何从该页面传递到另一个PHP生成的页面?我有:

  • A form on page 1 which sends data to a PHP script via POST.
  • Page 2 is generated by that script and shows one or more graphs generated by an external program using the entries on page 1 and the back end database. This page also has another form with options to re-generate the graphs with new options (e.g. zoom in on or truncate the graph(s)).
  • If requested, page 3 will be generated with the same PHP script using POST data glued together from pages 1 and 2. Except for the graphs, its basic appearance will be the same as page 2.
  • Pages 4, 5, 6 ... should be generated in the same manner as page 3.
  • 第 1 页上的表单,它通过 POST 将数据发送到 PHP 脚本。
  • 第 2 页由该脚本生成,显示由外部程序使用第 1 页上的条目和后端数据库生成的一个或多个图形。此页面还有另一种形式,其中包含使用新选项重新生成图形的选项(例如放大或截断图形)。
  • 如果请求,页面 3 将使用相同的 PHP 脚本生成,使用来自页面 1 和 2 的 POST 数据粘合在一起。 除了图表,其基本外观将与页面 2 相同。
  • 第 4、5、6 页……应该以与第 3 页相同的方式生成。

So, how can I put the POST data used to generate page 2 into the POST data for page 3?

那么,如何将用于生成第 2 页的 POST 数据放入第 3 页的 POST 数据中?

EDIT: Due to organizational policy, cookies can't be used (so sessions are not feasible). GET is undesirable because we don't want the input to show in the URL.

编辑:由于组织政策,不能使用 cookie(因此会话不可行)。GET 是不可取的,因为我们不希望输入显示在 URL 中。

回答by Soviut

I recall struggling with this issue long ago, wondering why I simply couldn't redirect with a modified POST header. The reason is a redirect is actually considered a GET.

我记得很久以前在这个问题上苦苦挣扎,想知道为什么我根本无法使用修改后的 POST 标头进行重定向。原因是重定向实际上被认为是 GET。

Regardless, you need to store the post variables in hidden fields.

无论如何,您需要将 post 变量存储在隐藏字段中。

<input type="hidden" name="someValueFromPageOne" value="blah">

I would recommend prefixing all your field names from each form so that its easy to tell them apart during your consolidation phase at the end.

我建议为每个表单中的所有字段名称添加前缀,以便在最后的整合阶段很容易区分它们。

<input type="hidden" name="pageOne_firstName" value="Joe">
<input type="hidden" name="pageTwo_streetNumber" value="22">

Edit: As others have mentioned, persisting data using sessions is one possibility, but this becomes a very complex matter of maintaining temporary state which things like page refreshes or using the back button can make difficult to maintain. Unless you're facing an extreme case, it's much easier to persist data using fields since they survive refreshes and other browser behaviour much more easily.

编辑:正如其他人所提到的,使用会话持久化数据是一种可能性,但这成为维护临时状态的一个非常复杂的问题,页面刷新或使用后退按钮之类的事情可能难以维护。除非您面临极端情况,否则使用字段保留数据要容易得多,因为它们更容易在刷新和其他浏览器行为中幸存下来。

回答by PTBNL

Wez Furlong recently wrote the php5 version on his blog (titled HTTP post from php, without cURL):

Wez Furlong 最近在他的博客上写了 php5 版本(标题是来自 php 的 HTTP 帖子,没有 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;
}

In the post he mentions that he always has to look up how to do this. Funny because he's one of the core developers!

在帖子中,他提到他总是必须查找如何做到这一点。有趣,因为他是核心开发人员之一!

回答by strager

Use GET.

使用获取。

In my opinion, POST requests should modify something (e.g. add records to a database). GET requests should retrieve something (e.g. results of a search query).

在我看来,POST 请求应该修改一些东西(例如将记录添加到数据库)。GET 请求应该检索一些东西(例如搜索查询的结果)。

If you want to use POST anyway, look into PHP sessions.

如果您无论如何都想使用 POST,请查看 PHP 会话。

回答by ?? .

Sessions are a pain, and if you needed them you'd already have implemented them.

会话是一种痛苦,如果您需要它们,您已经实现了它们。

As @Soviut said above, hidden input fields are probably the way to go for this.

正如@Soviut 上面所说,隐藏的输入字段可能是实现此目的的方法。

回答by dkretz

If you decide to bite off the session route with the dbms option, I've had luck designing a state class to hold this stuff, and serializing an object using JSON to a single large field in the session record.

如果您决定使用 dbms 选项取消会话路由,那么我很幸运地设计了一个状态类来保存这些内容,并使用 JSON 将对象序列化为会话记录中的单个大字段。