A-OK
A-OK

Reputation: 3254

PHP - header redirect with # breaks in MSIE

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?

Upvotes: 1

Views: 220

Answers (3)

Tahir
Tahir

Reputation: 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.

Upvotes: -1

Adam Fowler
Adam Fowler

Reputation: 1751

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

Upvotes: 0

CodeCaster
CodeCaster

Reputation: 151594

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.

Upvotes: 2

Related Questions