简体   繁体   中英

Firefox refreshes page 1, even after redirection to page 2

This is a quite weird and annoying problem, which is reproduced with the script below.

  1. Say we have two pages: script.php and script.php?second .
  2. Page 1 creates some database entries and redirects to page 2.
  3. On page 2, the user is presented with an editor for said entries.

If page 1 for some reason crashes on the first try, and prints some error message, a strange thing will happend. If we refresh page 1 (and this time it redirects fine), every consecutive refresh (of page 2) will actually refresh page 1 and again redirect to page 2.

In the above example this would create new database entries for every refresh, which is the problem I want to circumvent by redirecting to page 2.

<?php

header('Content-type: text/plain');

session_start();

if (!isset($_GET['second'])) {

    $_SESSION['counter'] = isset($_SESSION['counter']) ? $_SESSION['counter'] + 1 : 1;
    /*$_SESSION['counter'] = 0;
    exit('asd');*/
    header("Location: {$_SERVER['PHP_SELF']}?second", true, 303);
    exit;

}

echo "Counter: {$_SESSION['counter']}";

To try the above complete script, first run it with the commented code intact, then by enabling the commented code.

I've tried 301, 302 and 303 redirections. Does someone know why this is happening?

From RFC 2616 :

   10.3  Redirection 3xx .............................................61
   10.3.1   300 Multiple Choices .....................................61
   10.3.2   301 Moved Permanently ....................................62
   10.3.3   302 Found ................................................62
   10.3.4   303 See Other ............................................63
   10.3.5   304 Not Modified .........................................63
   10.3.6   305 Use Proxy ............................................64
   10.3.7   306 (Unused) .............................................64
   10.3.8   307 Temporary Redirect ...................................65

I think you're seeing the redirection cached in your browser, because you're using "permanent" redirects.

Try status code 307, or omit that parameter entirely to use the default (then find out what that is and let us know!)

You're checking if the get variable second is set. Yet you have not set it on your redirect.

Try

header("Location: {$_SERVER['PHP_SELF']}?second=1", true, 303);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM