2011-10-08 135 views
1

在表单上传之后使用header()重定向时,如果重定向中有#,它将在MSIE中消失,但在其他浏览器中正常工作。我做了以下简单的脚本为例:PHP - 在MSIE中带#个标头的标题重定向

<?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> 

在Firefox和重定向到http://localhost/#someanchor

在MSIE它重定向到http://localhost其他浏览器(失去锚)

如果我删除文件输入,那么它也可以在MSIE中使用! (但我需要文件上传)

我可以用Javascript解决它,但也许有什么我在这里失踪?

+1

我试图解决它,但无法弄清楚:P也许你可以用别的东西取代锚点,像一些获取请求?顺便说一句,使用'$ _SERVER [“PHP_SELF”]'是很危险的。 Google'php_self危险'了解更多信息。 – Olli

+0

谢谢,这只是一个简单的例子,我通常不使用PHP_SELF,但是当我这样做时,我会确保通过htmlspecialchars。 –

+1

好的,很好。但是,你可以使用GET请求来解决问题,而不是使用锚? – Olli

回答

2

这是IE严格执行RFC的极少数情况之一。在位置的头,你must发送 '绝对URI',定义here

absoluteURI = scheme ":" (hier_part | opaque_part)

因此,无论片段(#)。

请参阅this question以获取更详尽的答案。

0

不要将散列发送到标头位置。相反,使用元重定向。

-1

我发现下面的解决了这个问题:

而不是

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

我在Javascript中写道:

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

它工作在IE8。