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

查看:211
本文介绍了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(分析)中引荐来源的方式,并发现了这篇文章:

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.

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

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调用 之后的所有代码都将被执行.

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天全站免登陆