PHP 使用 POST 数据重定向 [英] PHP Redirect with POST data

查看:72
本文介绍了PHP 使用 POST 数据重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个话题做了一些研究,有一些专家说它不是可能,所以我想寻求替代解决方案.

I did some research on this topic, and there are some experts who have said that it is not possible, so I would like to ask for an alternative solution.

我的情况:

页面 A:[checkout.php] 客户填写他们的账单详情.

Page A: [checkout.php] Customer fills in their billing details.

页面 B:[process.php] 生成发票编号并将客户详细信息存储在数据库中.

Page B: [process.php] Generate an invoice number and store customer details in database.

页面 C:[thirdparty.com] 第三支付网关(仅接受发布数据).

Page C: [thirdparty.com] Third Payment Gateway (ONLY ACCEPT POST DATA).

客户在页面 A 中填写他们的详细信息并设置他们的购物车,然后发布到页面 B.在 process.php 中,将发布的数据存储在数据库中并生成发票编号.之后,将客户数据和发票编号发布到第三方网站支付网关.问题是在页面 B 中进行 POST.cURL 能够将数据 POST 到页面 C,但问题是页面没有重定向到页面 C.客户需要在页面 C 上填写信用卡详细信息.

Customer fills in their details and sets up their cart in Page A, then POSTs to Page B. Inside process.php, store the POSTed data inside the database and generate an invoice number. After that, POST the customer data and invoice number to thirdparty.com payment gateway. The problem is doing POST in page B. cURL is able to POST the data to Page C, but the problem is the page didn't redirect to page C. The customer needs to fill in Credit Card details on Page C.

第三方支付网关确实为我们提供了 API 示例,示例是将发票编号与客户详细信息一起发布.我们不希望系统生成过多不需要的发票编号.

The third party payment gateway did give us the API sample, the sample is POST the invoice number together with customer detail. We don't want the system to generate an excess of unwanted invoice numbers.

有什么解决办法吗?我们目前的解决方案是让客户在页面 A 中填写详细信息,然后在页面 B 中创建另一个页面,显示那里的所有客户详细信息,用户可以在其中单击确认"按钮发布到页面 C.

Is there any solution for this? Our current solution is for the customer to fill detail in Page A, then in Page B we create another page showing all the customer details there, where the user can click a CONFIRM button to POST to Page C.

我们的目标是让客户只需点击一次.

Our goal is for customers to only have to click once.

希望我的问题很清楚:)

Hope my question is clear :)

推荐答案

在页面 B 上生成一个表单,将所有必需的数据和操作设置为页面 C,并在页面加载时使用 JavaScript 提交它.您的数据将被发送到页面 C,不会给用户带来太多麻烦.

Generate a form on Page B with all the required data and action set to Page C and submit it with JavaScript on page load. Your data will be sent to Page C without much hassle to the user.

这是唯一的方法.重定向是一个 303 HTTP 标头,您可以在 http://www 上阅读.w3.org/Protocols/rfc2616/rfc2616-sec10.html,但我会引用其中的一些:

This is the only way to do it. A redirect is a 303 HTTP header that you can read up on http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html, but I'll quote some of it:

对请求的响应可以是在不同的 URI 下找到并且应该使用 GET 方法检索那个资源.这个方法存在主要是为了允许输出POST 激活脚本重定向用户代理到选定的资源.这新 URI 不是替代引用对于最初请求的资源.303 响应不能被缓存,但对第二个的回应(重定向)请求可能是可缓存.

The response to the request can be found under a different URI and SHOULD be retrieved using a GET method on that resource. This method exists primarily to allow the output of a POST-activated script to redirect the user agent to a selected resource. The new URI is not a substitute reference for the originally requested resource. The 303 response MUST NOT be cached, but the response to the second (redirected) request might be cacheable.

实现您正在做的事情的唯一方法是使用将用户发送到页面 C 的中间页面.以下是有关如何实现这一目标的小/简单片段:

The only way to achieve what you're doing is with a intermediate page that sends the user to Page C. Here's a small/simple snippet on how you can achieve that:

<form id="myForm" action="Page_C.php" method="post">
<?php
    foreach ($_POST as $a => $b) {
        echo '<input type="hidden" name="'.htmlentities($a).'" value="'.htmlentities($b).'">';
    }
?>
</form>
<script type="text/javascript">
    document.getElementById('myForm').submit();
</script>

您还应该在 noscript 标签内有一个简单的确认"表单,以确保没有 Javascript 的用户能够使用您的服务.

You should also have a simple "confirm" form inside a noscript tag to make sure users without Javascript will be able to use your service.

这篇关于PHP 使用 POST 数据重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆