1

I have just been wondering, say the user starts on the URL:

http://www.foo.com/

and then they click a link which sends them to say:

http://www.foo.com/some-page.php

but then inside this script you do:

header('Location: http://www.foo.com/another-age.php');

Now, what happens if the user presses the back button in their browser? Are they sent back to http://www.foo.com/some-page.php and then again redirected to http://www.foo.com/another-age.php making the back button useless or are they redirected back to http://www.foo.com/?

I know I can just test this out, but I thought some browsers may behave differently and wanted to know how it was supposed to work?

Brett
  • 16,869
  • 50
  • 138
  • 258

5 Answers5

2

It all depends on you and how you going to use in your code: header() is used to send a raw HTTP header (for mor detail go to http://php.net/manual/en/function.header.php). I use it, when I want to redirect the user to specific page in my web application (After the user finish a particular function).

Base on my knowledge, using header(), the browser don't enable the back buttom if don't have seconds to redirect.

Example with seconds:

header( "refresh:5;url=http://stackoverflow.com" );

header( "refresh:5;url=wherever.php" );

in other form to used, like you say, if a click in link tha go to: http://www.foo.com/some-page.php using heades(), this mean tha the link go to wharever.php that have the header code that go to some-page.php. with this, the back buttom will always be available. other way to used is with if or calling a specific function.

for other hand, if you don't want any save cache in any browser that use to navigate your web page or explanation of how work and how disable, there is a good post here (have examples in different script langues ):How to control web page caching, across all browsers?

Example with head() PHP:

header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.
Community
  • 1
  • 1
XOXE
  • 31
  • 5
1

A post from W3C titled Use standard redirects: don't break the back button! is obviously says that: unless the redirection is from "refresh" technique (as opposed, HTTP redirect) browser wouldn't catch the redirector.

Quote from that post about HTTP Redirect

A "HTTP Redirect" on the other hand acts much more directly because it is done within another layer. When the User Agent (i.e. a browser or the validator) first contacts the server and requests the document, the server itself, having been configured to redirect the document to another address, replies to the user-agent that it should instead look at the new address.

That specifically telling /some-page.php is moved to /another-page.php (I somewhat thinking that they're could also said as 'identical'). Since then, as per question, browser back button will goes to foo.com directly.

About this function

header('Location: http://www.foo.com/another-page.php');

It uses Location header which you can read about it from spec here.


As for testing, you can try any URL shortener such as goo.gl. It implements 301 redirection. You won't get the shortened URL back once you visit the URL with browser back button. Or just go to fb.com, you'll be redirected to facebook.com and sure won't go back to the former URL (what's the point of domain masking if you're not actually hidding it?)

A note from Wikipedia, another way to push redirector to browser history is by JavaScript

window.location='http://www.example.com/'
Chay22
  • 2,545
  • 2
  • 13
  • 21
0

If you want to try in browsers, the following script will create three files you can use.

<?php

$dir = '/tmp/';
foreach(
    array(
        'foo.php' => '<a href="bar.php">Go to bar.</a>',
        'bar.php' => '<?php header("Location: baz.php");',
        'baz.php' => '<h1>Hello I\'m Baz</h1>'
    )
    as $name => $contents
) {
    file_put_contents($dir . $name, $contents);
}

Once you have your three files, place somewhere web accessible. To use open foo.php and click on the link.

In Chrome 46, hitting back from baz.php (after the redirect), takes me back to foo.php.

In Firefox Nightly 50 and Opera 12, same as above.

Progrock
  • 6,765
  • 1
  • 16
  • 25
  • Thanks for running those tests - I guess I'm just trying to find out if this is standard behaviour and hence my question rather than running tests like this myself :) – Brett Jul 05 '16 at 11:16
0

This will redirect exactly to the path which you have defined in header function that is http://www.foo.com/another-age.php. You may redirect it to home page also and you can visit the path by using back button.

-1

This will make the back button useless. It will redirect them back to "http://www.foo.com/another-age.php"

(Atleast with my simple experience)

NIDOIT
  • 45
  • 6