重定向到带有 POST 数据的新页面 (PHP/Zend)

发布于 2024-08-03 07:52:48 字数 977 浏览 8 评论 0原文

我试图弄清楚如何重定向到新页面(不同的应用程序、控制器、操作)并维护当前的 POST 数据。

我的应用程序正在验证 POST 数据,如果满足特定条件,我想重定向到不同的位置,但请确保 POST 数据作为 POST 数据传递到该新页面。这是一个粗略的示例:

POST /app1/example HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 17
    var1=foo&var2=bar

在我的 exampleAction (Zend_Controller_Action) 中,我检查是否 var1 == foo,如果是,我想将 (302) 重定向到 /app2 /example 具有相同的 POST 数据。也许是这样的?

HTTP/1.x 302 Found
Location: /app2/example
Content-Type: application/x-www-form-urlencoded
Content-Length: 17
    var1=foo&var2=bar

我可以了解如何使用 Zend_Http_Client 创建新的 HTTP 请求,但我不想简单地请求页面并显示内容。我仍然应该使用 Zend_Http_Client 吗?现在我正在玩这样的东西:

$this->getResponse()->setRedirect('/app2/example/', 302);
$this->getResponse()->setBody(http_build_query($this->_request->getPost()));

我确信我想做的事情可以通过一些诡计和妖魔鬼怪的手段来实现,但这绝对是在暗示我。任何帮助表示赞赏。

- rk

I'm trying to figure out how to redirect to a new page (different application, controller, action) and maintain the current POST data.

My app is validating the POST data and if a certain criteria is met, I want to redirect to a different location, but make sure the POST data is passed as POST data to that new page. Here's a rough example:

POST /app1/example HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 17
    var1=foo&var2=bar

In my exampleAction (Zend_Controller_Action), I check to see if var1 == foo, and if it does I want to redirect (302) to /app2/example with that same POST data. Something maybe like this?

HTTP/1.x 302 Found
Location: /app2/example
Content-Type: application/x-www-form-urlencoded
Content-Length: 17
    var1=foo&var2=bar

I can see how to create a new HTTP request using Zend_Http_Client, but I'm not looking to simply request the page and display the content. Should I still be looking to use Zend_Http_Client anyway? At the moment I'm playing with stuff like this:

$this->getResponse()->setRedirect('/app2/example/', 302);
$this->getResponse()->setBody(http_build_query($this->_request->getPost()));

I'm sure what I want to do is possible through some means of trickery and demon-cavorting, but it's definitely alluding me. Any help is appreciated.

- rk

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

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

发布评论

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

评论(6

究竟谁懂我的在乎 2024-08-10 07:52:49

与其将浏览器重定向到新位置,为什么不将请求从控制器转发到另一个控制器呢?

return $this->_forward("action", "controller", "module");

Rather than redirecting the browser to the new location, why not just forward the request from the controller to the other controller?

return $this->_forward("action", "controller", "module");
还如梦归 2024-08-10 07:52:49

IMO 你不能用 POST 进行重定向。请参阅http://www.w3.org/Protocols/rfc2616/ rfc2616-sec10.html#sec10.3

正如已经说过的,您可以在会话中保存 POST 数据。

IMO you can't do a Redirect with POST. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3

As already said you could save your POST-Data in the Session.

薄荷→糖丶微凉 2024-08-10 07:52:49

您可以使用 Controller _request 对象

$this->_request->setPost(array(
    'someKey' => 'someValue'
));

,然后只需使用 _forward 受保护方法

$this->_forward('yourAction', 'yourController', 'yourModule');

然后,在转发控制器操作中只需调用

$aPost = $this->_request->getPost();

You can use a Controller _request object

$this->_request->setPost(array(
    'someKey' => 'someValue'
));

And then simply use _forward protected method

$this->_forward('yourAction', 'yourController', 'yourModule');

Then, in forwarded controller action just call

$aPost = $this->_request->getPost();
山田美奈子 2024-08-10 07:52:49

您可以使用重定向器操作助手:

http://framework.zend.com/manual/1.12/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.redirector

这对我有用。它将登录凭据从一个登录屏幕传递到另一个登录屏幕。 307 可能会导致浏览器警告用户正在发生重定向,303 将重定向并在下一页上填充凭据,但不提交第二个表单。

$baseUrl = 'https://example.com';
$this->_redirector = $this->_helper->getHelper('Redirector');
$this->_redirector->setCode(307)
   ->setUseAbsoluteUri(true)
   ->gotoUrl($baseUrl . '/home/login',
        array('username' => $authForm->getValue('username'),
              'password' => $authForm->getValue('password')
        )
   );

You can use the redirector action helper:

http://framework.zend.com/manual/1.12/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.redirector

This is working for me. It is passing the login credentials from one login screen to another. The 307 may cause the browser to alert the user that the redirect is occurring, a 303 will redirect and populate the credentials on the next page, but not submit the second form.

$baseUrl = 'https://example.com';
$this->_redirector = $this->_helper->getHelper('Redirector');
$this->_redirector->setCode(307)
   ->setUseAbsoluteUri(true)
   ->gotoUrl($baseUrl . '/home/login',
        array('username' => $authForm->getValue('username'),
              'password' => $authForm->getValue('password')
        )
   );
灯角 2024-08-10 07:52:49

因为客户端在服务器响应后处理重定向,所以我认为您对此无能为力。例如,Firefox 会询问用户是否要将 POST 数据重新发送到新位置。

一个糟糕的解决方案是填写并返回 HTML 表单,然后使用 JavaScript 自动提交。 :/

Because the client handles the redirection after the server's response I don't think you can do much about this. Firefox for example asks the user whether he/she wants to resend the POST data to the new location or not.

A nasty solution would be to fill and return a HTML form and auto-submit it with JavaScript. :/

寄人书 2024-08-10 07:52:49

我对 Zend 不太熟悉,但有两种方法可以解决您的问题:

  1. 在请求/重定向的生命周期中将 POST 数据存储在会话中
  2. 将重定向设置为 POST,并对数据进行编码。不使用重定向,只需使用 POST 而不是 GET 发送新标头。

I'm not too familiar with Zend specifically, but there are two approaches I'd take to your problem:

  1. Store the POST data in the session for the life of that request/redirect
  2. Set the redirect to be a POST, and encode the data. Instead of using redirect, just send a new header with POST instead of GET.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文