你在哪里使用命令 header()?
我在 handlers/handle_login.php 中有以下代码.用户从 index.php
进入站点,这是起始位置.
I have the following code at handlers/handle_login.php. The user has gone to the site from index.php
which is the starting place.
if(!$logged_in){
header("Location: index.php");
die("You are not logged_in");
}
如果 if-clause 为真,我会收到 404 错误,因为标题将我转到 handlers/index.php
,而不是 index.php
.
If if-clause is true, I get a 404 error, since the header puts me to to handlers/index.php
, instead of index.php
.
虽然我同意尼拉莫和伯爵的观点,但我希望我能给出更大的图景:
While I agree with nilamo and earl, I hope I can give a bigger picture:
使用相对路径可能会产生非常奇怪的效果,具体取决于浏览器的位置认为"它在您的站点层次结构中.例如,假设站点有一个索引文件/index.php",但配置为接受 URI 路径中的模块和操作.您可能有一个如下所示的网址:
Using relative paths can have very strange effects depending on where the browser 'thinks' it is in your site hierarchy. For example, assume the site has an index file '/index.php' but is configured to accept module and action in the URI path. You may very well have a url that looks like:
http://www.yoursite.com/forms/contact/
从这种情况下,返回一个像这样的标题:
From this situation, returning a header like:
header("Location: index.php");
很可能会导致浏览器尝试请求
may very well cause the browser to try to request
http://www.yoursite.com/forms/contact/index.php
这显然不是你想要的.出于这个原因,通常最好使用上面推荐的/index.php",或者尽可能使用完全限定的 URL.
which is obviously not what you want. For this reason, it's generally better to use '/index.php' as recommended above, or even better use the fully qualified URL when possible.
希望这会有所帮助.
这篇关于了解 PHP 的 header()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!