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

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.

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
  // 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.

I advice to never use HTTP_REFERER.

  1. It is possible it isn't set at all.
  2. It can be anything, don't expect your previous page.

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.

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.

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'] !

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.

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:

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