PHP:标题后的代码效果(“位置:abc.html”) [英] PHP: Effect of code after header("Location: abc.html")

查看:99
本文介绍了PHP:标题后的代码效果(“位置:abc.html”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,代码看起来像这样:

Lets say, the code looks something like this:

if(!$test) {
  header("Location: somefile.html");
  ...some PHP code....
  header("Location: anotherfile.html");
}

上面是'某些PHP代码'吗?如果是,那么其中的HTTP响应会发生什么(例如:代码中的第二个'header'语句)?

Is 'some PHP code' above executed? If yes, then what happens to further HTTP response(s) therein (eg.: the second 'header' statement in the code)?

推荐答案

- 代码将被执行。

Yes - the code will be executed.

标头()配置要返回的标头,而不是立即发送。


  • 如果有2
    来电之间的无输出,只有最后一个才会考虑

  • If there is no output between the 2 calls, then only the last one will be taken into account.

但是,如果您在第二次通话前输出任何
,则会发送
标题,并且第二次
通话将导致错误(标头
已经发送)。

However, if you output anything before the second call, then the headers will be sent, and the second call will result in an error (headers already sent).

经典错误是:重定向而不是 exit()之后,这可能会导致安全问题。

A classic mistake is : redirect and not exit() right after that, which can cause security problems.

这篇关于PHP:标题后的代码效果(“位置:abc.html”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆