Viewed   274 times

The title of this question kind of explains my question. How do I redirect the PHP page visitor back to their previous page with the header( "Location: URL of previous page" );

 Answers

1

try:

header('Location: ' . $_SERVER['HTTP_REFERER']);

Note that this may not work with secure pages (HTTPS) and it's a pretty bad idea overall as the header can be hijacked, sending the user to some other destination. The header may not even be sent by the browser.

Ideally, you will want to either:

  • Append the return address to the request as a query variable (eg. ?back=/list)
  • Define a return page in your code (ie. all successful form submissions redirect to the listing page)
  • Provide the user the option of where they want to go next (eg. Save and continue editing or just Save)
Wednesday, November 9, 2022
3
<?php
    header("Status: 301 Moved Permanently");
    header("Location:./content/index.html?". $_SERVER['QUERY_STRING']);
    exit;
?>
Tuesday, September 27, 2022
 
2

http://book.cakephp.org/view/430/referer

Use a hidden <input> field that holds the initial referrer and gets submitted with the login data.

Thursday, August 11, 2022
2

It seems that your nginx configuration is causing the problems.

Its totally possible that nginx is modifying the response headers. This is not by default - you could have a configuration that is aimed for it to behave as a reverse proxy etc.

Have you tried testing the redirect on a nginx with its default configuration?

Thursday, September 29, 2022
4

could the code after the header-location call be effectively executed?

Yes, always. The header is only a line of data asking the browser to redirect. The rest of the page will still be served by PHP and can be looked at by the client by simply preventing the header command from executing.

That is easy enough to do with a command-line client like wget, for example, by simply telling it not to follow redirects.

Bottom line: If you don't prevent it, PHP will send out the whole body even after a header call. That body is fully available to the recipient without any special hacking skills.

Wednesday, December 7, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :