开发者

PHP - header redirect with # breaks in MSIE

开发者 https://www.devze.com 2023-04-11 09:38 出处:网络
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:

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?


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消