PHP中重定向标头之后的所有代码都会被执行吗? [英] Will all code after redirect header in PHP always get executed?

查看:21
本文介绍了PHP中重定向标头之后的所有代码都会被执行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道一般的经验法则是在 PHP 中进行标头重定向之后,您应该调用 exit() 以避免运行额外的代码,但我想知道您是否将代码放在重定向标头之后,如果它会总是跑步?

So I know the general rule of thumb is after doing a header redirect in PHP, you should call exit() to avoid having extra code running, but I want to know if you put code after the redirect header, if it will always run?

我正在对 Google Analytics 中跟踪推荐的各种方法进行一些研究,并发现了这篇文章:谷歌分析提示和技巧 - 在 Google Analytics 中跟踪 301 重定向

I was doing some research on various ways of tracking referrals in Google Analytics and came across this post: Google Analytics Tips & Tricks – Tracking 301 Redirects in Google Analytics

它建议做这样的事情:

<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" );
?>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src=’" + gaJsHost + "google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-YOURPROFILE-ID");
pageTracker._trackPageview();
} catch(err) {}</script>

从我一直理解 header() 函数的方式来看,它取决于浏览器,它可以随时运行重定向.因此,不能保证 JavaScript 会在重定向发生之前实际开始或完成执行.

From the way I've always understood the header() function, it's up to the browser and it can run the redirect whenever it wants to. So there's no guarantee the JavaScript would actually begin or finish executing prior to the redirect occurring.

PHP 的 header() 函数文档 说明了退出的原因重定向是确保在我们重定向时不会执行下面的代码".这听起来不像他们保证所有以下代码都会运行,只是它可能会发生.

PHP's documentation for the header() function indicates the reason for exiting after a redirect is to "make sure that code below does not get executed when we redirect." That doesn't sound like they guarantee all following code will run, just that it could happen.

不管怎样,我找到了一种不同的方法来实际管理跟踪,但我想看看我是否能找出在这种情况下 header() 究竟是如何工作的..

Regardless, I found a different way to actually manage the tracking, but I wanted to see if I could find out how exactly header() worked in this situation..

感谢您的帮助.

推荐答案

在 PHP 中使用 header 函数只会添加到服务器返回的响应的 headers.它不会立即发送任何数据,也不会立即终止连接.header 调用 之后的任何代码都会被执行.

Using the header function in PHP only adds to the headers of the response returned by the server. It does not immediately send any data and does not immediately terminate the connection. Any code after the header call will be executed.

特别是,即使在执行 301 重定向之后添加响应正文也是一个好主意,这样不支持重定向的客户端也会得到一些描述性响应.事实上,根据 HTTP 1.1 规范 第 10.3.2 节 -

In particular, it's a good idea to add a response body even after doing a 301 redirect so that clients that do not support the redirect also get some descriptive response. Infact according to the HTTP 1.1 specification Section 10.3.2 -

除非请求方法是 HEAD,否则响应的实体应该包含一个简短的超文本注释,其中包含指向新 URI 的超链接.如果收到 301 状态代码以响应请求,而不是GET 或 HEAD,用户代理不能自动重定向请求,除非它可以被用户确认,因为这可能更改发出请求的条件.

Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s). If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

这篇关于PHP中重定向标头之后的所有代码都会被执行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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