1

I know I can use this to go back to the previous page:

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

But I need to go back to the page before that. I want to achieve this:

Page 1 -> Insert record page with form -> submit form -> handle insert in php -> redirect to page 1.

Right now, using the code above will only take me back to my "Insert record page with form" = second page, not the first. Can it be done?

Buffalo
  • 3,861
  • 8
  • 44
  • 69

2 Answers2

3

Try this:

Change in your "handle inser...":

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

Add in your "insert record..."

if(isset($_GET["redirect"]) { header ("location: page1.html"); }

Explanation:

The first one will send a parameter to the "insert record page then the insert record page will check if it has a parameter named "redirect" then if it will be having it will redirect it to the page1!


I would recommend you creating a seprate page for redirection where you send it a URL as a parameter and it redirects you to that particular URL. That's what I do. Meanwhile on that page I also do some validation. :)

According to @War10ck and PHP Docs:

This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

For that this question may help. It suggests keeping the record our selves.

Resource: PHP Docs

Community
  • 1
  • 1
Mohammad Areeb Siddiqui
  • 9,795
  • 14
  • 71
  • 113
  • Thanks, simple approach but not so clean (you hardcoded "1"): I should pull the "1" from the GET request and use it in header ("location: ..."), I see. I will accept the answer in a few minutes. – Buffalo Jul 05 '13 at 20:44
  • Yeah, I know I hardcoded it! It's of no use you could just remove it and still if will work! :) – Mohammad Areeb Siddiqui Jul 05 '13 at 20:45
  • 2
    As a side note, per the [PHP Docs for `$_SERVER['HTTP_REFERRER']`](http://php.net/manual/en/reserved.variables.server.php): _"This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted."_ Don't forget to take that into account as people may browse your site using different browsers or even different devices altogether. This variable may not turn out to be a reliable solution across platforms. – War10ck Jul 05 '13 at 20:52
  • Thanks War10ck! I'll make sure to replace it. – Buffalo Jul 05 '13 at 21:09
1

It can be done, but not the way you are attempting. The web history of the client is hidden on purpose, for privacy reasons. You can see the referrer, but that's it. See @MohammadAreebSiddiq's answer for an alternate design suggestion. Good luck!

Liam
  • 1,041
  • 2
  • 17
  • 31
  • 1
    I must say I'm surprised how everything can be achieved with PHP, coming from an Android environment where stuff breaks all the time and even implementing navigation is a major PITA. – Buffalo Jul 05 '13 at 21:10