问题描述
在handlers / handle_login.php中有以下代码:
用户已经从 index.php
这个出发地去了网站。
if(!$ logged_in){
header(Location:index.php);
die(You are not logged_in);
$ b如果if-clause为真,我会得到一个404错误,让我去 handlers / index.php
,而不是 index.php
。
虽然我同意nilamo和伯爵,但我希望能给出一个更大的图片:
使用
解决方案相对路径可能会产生非常奇怪的效果,具体取决于浏览器
'认为'它在您的网站层次结构中的位置。例如,假设该站点有一个索引文件'/index.php',但被配置为接受URI路径中的模块和动作。您可能会看到如下所示的网址:
http://www.yoursite.com/forms/contact/
在这种情况下,返回一个标头,如:
header(Location:index.php);
可能会导致浏览器尝试请求
http://www.yoursite.com/forms/contact/index.php
这显然不是你想要的。出于这个原因,通常最好使用上面推荐的'/index.php',或者更好地使用完全限定的URL。
/ p>
Where do you use the command header()?
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 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:
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
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.
Hope this helps.
这篇关于为了理解PHP的header()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!