我的网址是 http://www.website.ro/index.php?subiect=10046&pag=1 #1006,我这里有一个没有任何操作的表单 (<form action="" method="post">
)。
提交后我正在检查一些输入字段,如果有任何错误,我想重定向。我使用这段代码:
header('Location: /index.php?subiect='.$_GET['subiect'].'&pag='.$_GET['pag']); exit();
但是重定向包括#1006 所以我的页面变成http://www.website.ro/index.php?subiect=10046&pag=1 #1006
我尝试更改 header 中的位置:
header('Location: http://www.google.com'); exit();
带我去的页面是:http://www.google.com #1006
为什么会这样?
我有 ob_start()
在第一行,我试图删除它。没有效果。重定向后的 url 仍然有 #1006。
如果我的网址是http://www.website.ro/index.php?subiect=10046&pag=1我使用 header 位置我没有问题,但如果 url 包含#1006,重定向后的 url 包含到。
最佳答案
#
之后的部分称为片段标识符,用于标识页面的一部分,即使该页面是从另一个 URL 检索到的也是如此。
默认情况下,大多数浏览器会在发生 3xx 重定向时保留片段标识符,而不管使用什么状态代码。参见 URL Fragment and 302 redirects有关此事的一些资源。
您可以返回一个包含片段标识符本身的 URI(即使是空标识符):
header('Location: http://www.google.com/#');
然后将使用那个,如 this draft 中指定的那样:
The exception is when a returned URI already has a fragment identifier. In that case the original fragment identifier MUST NOT be not added to it.
您还可以使用 Javascript 更改片段标识符 onclick
。
关于php - header 位置无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14076913/