1

When redirecting with header() after a form upload, if there is # in the redirect, it disappears in MSIE, but works properly in other browsers. I've made the following simple script as an example:

<?php
if (isset($_REQUEST["description"])) {
    $location = "http://localhost/#someanchor";
    header("Location: $location");
    exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>PHP header redirect with #</title>
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
        <div>
            <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER["PHP_SELF"]?>">
                <div>
                    Description <input type="text" name="description" /><br /><br />
                    File <input type="file" name="uploadfile" /><br /><br />
                    <input type="submit" />
                </div>
            </form>
        </div>
    </body>
</html>

In Firefox and other browsers it redirects to http://localhost/#someanchor

In MSIE it redirects to http://localhost (loses the anchor)

If I remove the file input, then it works in MSIE as well! (but I need the file upload)

I could work around it with Javascript but maybe there is something I'm missing here?

A-OK
  • 3,184
  • 10
  • 34
  • 42
  • 1
    I tried to solve it but cannot figure it out :P Maybe you could replace anchor with something else, like some get request? BTW, it´s dangerous to use `$_SERVER["PHP_SELF"]`. Google `php_self danger` to find out more. – Olli Oct 08 '11 at 10:38
  • Thanks, this was just a quick example, I usually don't use PHP_SELF but when I do I'll make sure to put it through htmlspecialchars. – A-OK Oct 08 '11 at 10:45
  • 1
    ok, good. But can you do the solution using GET request, not with anchors? – Olli Oct 08 '11 at 10:52
  • well the anchor is generated based on form input and after the form is submitted so that's not exactly what I need. Still I can easily do something with Javascript – A-OK Oct 08 '11 at 16:00

3 Answers3

2

This is one of the rare cases where IE stricly implements the RFC. In the Location-header, you must send an 'absolute uri', as defined here:

absoluteURI = scheme ":" ( hier_part | opaque_part )

So, no fragment (#).

See this question for a more extensive answer.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

Don't send the hash over a header location. Instead, use a meta-redirect.

Adam Fowler
  • 1,750
  • 1
  • 17
  • 18
-1

I found the following solution to this problem:

Instead of

header("http://localhost/#bottom");

I wrote in Javascript:

<script>
  function go2url() { 
     window.location='http://localhost/#bottom';
  }
  window.setTimeout('go2url();', 200);
</script>

It works in IE8.

kapa
  • 77,694
  • 21
  • 158
  • 175
Tahir
  • 1