成功完成任务后,在上一页重定向用户

时间:2022-10-20 08:25:06

What should be good way to redirect user after successful action like edit or delete any item, in delete condition we could use $_SERVER['HTTP_REFERER'] but in case of edit, we show first of all edit form and then user clicks update, so if we use same approach user would be redirected to edit page not main page. I hope you got idea about my confusion. thanks.

在编辑或删除任何项目成功操作后重定向用户应该是什么好方法,在删除条件下我们可以使用$ _SERVER ['HTTP_REFERER']但是在编辑的情况下,我们首先显示编辑表单,然后用户点击更新,因此,如果我们使用相同的方法,用户将被重定向到编辑页面而不是主页面。我希望你对我的困惑有所了解。谢谢。

5 个解决方案

#1


1  

If 'update' is handled through a Form post, just put a hidden input in reflecting the landing spot.

如果通过表格帖子处理“更新”,只需将隐藏的输入反映到着陆点。

<input type="hidden" name="next_url" value="foo.php" />

The PHP uses the value as the place to redirect to.

PHP使用该值作为重定向的位置。

<?php
  // Do operations.
  header('Location: '.$_REQUEST['next_url']);

This is especially important because HTTP_REFERER is not guaranteed to work. It works most of the time, but there are issues with relying on it.

这一点尤其重要,因为不保证HTTP_REFERER可以正常工作。它大部分时间都有效,但依赖它有一些问题。

#2


1  

I advice to never use HTTP_REFERER.

我建议永远不要使用HTTP_REFERER。

  1. It is possible it isn't set at all.
  2. 它可能根本没有设置。

  3. It can be anything, don't expect your previous page.
  4. 它可以是任何东西,不要指望你以前的页面。

I advice an solid solution using (as above mentioned) hidden field, session storage, or simply have a solid routing in your application so you know which route someone took. Pick the best.

我建议一个可靠的解决方案使用(如上所述)隐藏字段,会话存储,或者只是在您的应用程序中有一个可靠的路由,以便您知道某人采取了哪条路由。选择最好的。

#3


1  

If you're OK with $_SERVER['HTTP_REFERER'] you can simply store that value in Session Information, and redirect to it when editing is finished.

如果你对$ _SERVER ['HTTP_REFERER']没问题,你只需将该值存储在会话信息中,并在编辑完成后重定向到它。

For example, before showing the edit form you can do:

例如,在显示编辑表单之前,您可以执行以下操作:

$_SESSION['originalReferer'] = $_SERVER['HTTP_REFERER'];

And after clicking "Update" (if successful):

点击“更新”后(如果成功):

header("Location: ".$_SESSION['originalReferer']);

But again, use this only if you trust $_SERVER['HTTP_REFERER'] !

但是,只有当你信任$ _SERVER ['HTTP_REFERER']时才使用它!

#4


1  

Similar to the previous answer, I would suggest keeping track of the page you wish to redirect to using sessions., and then once you want to redirect, just use

与之前的答案类似,我建议跟踪您希望重定向到使用会话的页面。然后,一旦您想要重定向,只需使用

header('Location: '.$redirect_var);

The reason why I would put it in the session, rather than posting in a form, is that form posting can be manipulated by the user, whereas sessions are server side, and are entirely under your own control.

我之所以将它放在会话中而不是在表单中发布,是因为表单发布可以由用户操作,而会话是服务器端,完全由您自己控制。

#5


0  

Save the url in session when you are coming to Edit page and when you submit just use the below snippet after executing the update query:

当您进入“编辑”页面时,在会话中保存URL,并且在您提交后,只需在执行更新查询后使用以下代码段:

header('Location: '.$_SESSION['redirect_url']);

#1


1  

If 'update' is handled through a Form post, just put a hidden input in reflecting the landing spot.

如果通过表格帖子处理“更新”,只需将隐藏的输入反映到着陆点。

<input type="hidden" name="next_url" value="foo.php" />

The PHP uses the value as the place to redirect to.

PHP使用该值作为重定向的位置。

<?php
  // Do operations.
  header('Location: '.$_REQUEST['next_url']);

This is especially important because HTTP_REFERER is not guaranteed to work. It works most of the time, but there are issues with relying on it.

这一点尤其重要,因为不保证HTTP_REFERER可以正常工作。它大部分时间都有效,但依赖它有一些问题。

#2


1  

I advice to never use HTTP_REFERER.

我建议永远不要使用HTTP_REFERER。

  1. It is possible it isn't set at all.
  2. 它可能根本没有设置。

  3. It can be anything, don't expect your previous page.
  4. 它可以是任何东西,不要指望你以前的页面。

I advice an solid solution using (as above mentioned) hidden field, session storage, or simply have a solid routing in your application so you know which route someone took. Pick the best.

我建议一个可靠的解决方案使用(如上所述)隐藏字段,会话存储,或者只是在您的应用程序中有一个可靠的路由,以便您知道某人采取了哪条路由。选择最好的。

#3


1  

If you're OK with $_SERVER['HTTP_REFERER'] you can simply store that value in Session Information, and redirect to it when editing is finished.

如果你对$ _SERVER ['HTTP_REFERER']没问题,你只需将该值存储在会话信息中,并在编辑完成后重定向到它。

For example, before showing the edit form you can do:

例如,在显示编辑表单之前,您可以执行以下操作:

$_SESSION['originalReferer'] = $_SERVER['HTTP_REFERER'];

And after clicking "Update" (if successful):

点击“更新”后(如果成功):

header("Location: ".$_SESSION['originalReferer']);

But again, use this only if you trust $_SERVER['HTTP_REFERER'] !

但是,只有当你信任$ _SERVER ['HTTP_REFERER']时才使用它!

#4


1  

Similar to the previous answer, I would suggest keeping track of the page you wish to redirect to using sessions., and then once you want to redirect, just use

与之前的答案类似,我建议跟踪您希望重定向到使用会话的页面。然后,一旦您想要重定向,只需使用

header('Location: '.$redirect_var);

The reason why I would put it in the session, rather than posting in a form, is that form posting can be manipulated by the user, whereas sessions are server side, and are entirely under your own control.

我之所以将它放在会话中而不是在表单中发布,是因为表单发布可以由用户操作,而会话是服务器端,完全由您自己控制。

#5


0  

Save the url in session when you are coming to Edit page and when you submit just use the below snippet after executing the update query:

当您进入“编辑”页面时,在会话中保存URL,并且在您提交后,只需在执行更新查询后使用以下代码段:

header('Location: '.$_SESSION['redirect_url']);