使用 PHP 通过 HTTP Post 而非简单链接进行重定向

发布于 2024-11-01 04:57:36 字数 361 浏览 2 评论 0原文

我有一个杂志出版商的网站,该网站有一个标准超链接,指向另一个托管该杂志电子版的网站。电子杂志托管网站有其 owm、每个出版物、身份验证方案、每个用户每个杂志。为了避免杂志订阅者必须同时登录两个网站,双方同意采取某种握手方式。

当重定向到杂志托管站点时,我必须发布一个包含一些值的小型 HTML 表单。我的第一个障碍是杂志出版商网站是 CMS 驱动的,所以我不能真的只用输入包围一个按钮并将其伪装成 POST 重定向。

到目前为止,我最干净的解决方案是创建一个新的、非公开的、小型的 PHP,原始杂志网站将链接到该 PHP。该 PHP 将呈现一个小型 HTML 表单,该表单将立即将 instelf 发布到杂志托管站点,因此当杂志读者作为杂志托管站点到达时,他已经进行了身份验证

I have one website, of a magazine publisher, and this site has a standard hyperlink to another site that hosts the electronic version of the magazine. The emagazine hosting site has it's owm, per publication, authentication scheme, per user per mag. In order to avoid magazine subscribers having to sign on to both sites, the parties have agreed on a kind of handshaking.

When redirecting to the magazine host site, I must post a small HTML form with a few values. My first obstacle is that the magazine publisher site is CMS driven, so I can't really just surround one button with inputs and disguise it as a POST redirect.

My cleanest solution so far to create a new, non public, little PHP that the originals magazine site will link to. This PHP will render a small HTML form that will immediately post instelf to the magazine hosting site, so when the magazine reader arrives as the magazine hosting site he is already auththenticate

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

z祗昰~ 2024-11-08 04:57:36

这可能需要在服务器端使用 cURL(或带有 $context 参数的 file_get_contents)或在客户端使用 $.post.我认为您不会仅仅因为服务器标头告诉它就说服大多数浏览器提交 POST 请求 - 这对于安全性来说是非常糟糕的。 (例如:如果您正在观看价值 10,000 美元的电视页面,亚马逊可以简单地告诉您的浏览器单击“立即购买”按钮。)

PHP 示例

<?php
$opts = array(
  'http'=>array(
    'method'=>"POST"
    )
  );

$context = stream_context_create($opts);

$response = file_get_contents("<your url>", 0, $context);

JS 示例

params = { 'test1': 'value' };

$.post('<your url>', params, function(response)
{
    // handle response
});

This probably needs to be done on the server side using cURL (or file_get_contents with a $context parameter) or on the client side using a $.post. I don't think you're going to convince most browsers to submit a POST request just because a server header told it to - it'd be awful for security. (For example: Amazon could simply tell your browser to click the "Buy It Now" button if you were on the page of a $10,000 TV.)

PHP example

<?php
$opts = array(
  'http'=>array(
    'method'=>"POST"
    )
  );

$context = stream_context_create($opts);

$response = file_get_contents("<your url>", 0, $context);

JS Example

params = { 'test1': 'value' };

$.post('<your url>', params, function(response)
{
    // handle response
});
温柔嚣张 2024-11-08 04:57:36

您可以通过页面上的标头重定向来执行此操作,但它与表单提交不同,因此发布到的页面需要能够接受 GET 样式的表单提交,但您应该能够执行此操作使用如下代码:

<?php
$val1 = mysql_escape_string($_POST["val1"]);
$val2 = mysql_escape_string($_POST["val2"]);

//Authenticate Locally here and then...

$val1 = urlencode($val1);
$val2 = urlencode($val2);
header("Location: http://www.emagazine.com/forminput.php?val1=$val1&val2=$val2");

请记住,如果您向浏览器打印任何内容,则 php 标头重定向将失败,这需要是第一个到达浏览器的内容。

You can do this with a header redirect on the page but it won't be the same as a form submission, so the page its posting to will need to be able to accept GET style form submissions, but you should be able to do this with code like the following:

<?php
$val1 = mysql_escape_string($_POST["val1"]);
$val2 = mysql_escape_string($_POST["val2"]);

//Authenticate Locally here and then...

$val1 = urlencode($val1);
$val2 = urlencode($val2);
header("Location: http://www.emagazine.com/forminput.php?val1=$val1&val2=$val2");

Keep in mind if you print anything to the browser, the php header redirect will fail, this needs to be the first thing to hit the browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文