PHP - 重定向延迟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14955526/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Delay on redirect
提问by CustomNet
I'm using the following code to use as a form of nulling referring script and it works perfectly but it just redirects them straight to the target URL.
我使用以下代码作为归零引用脚本的一种形式,它运行良好,但它只是将它们直接重定向到目标 URL。
How would I go about creating a 5 second delay so I can display some adverts for 5 seconds before redirecting them?
我将如何创建 5 秒的延迟,以便在重定向广告之前显示 5 秒的广告?
回答by Vahe Shadunts
You can send php header with timeout refresh. http://php.net/manual/en/function.header.php
您可以发送带有超时刷新的 php 标头。http://php.net/manual/en/function.header.php
<?php 
  header( "refresh:5; url=wherever.php" ); 
?>
回答by fedorqui 'SO stop harming'
What about using sleep()?
使用sleep()怎么样?
function method1(...) {
sleep(5);
... rest of the code
Note however that it is more recommended to use Vahe Shadunts's answer, which uses header()instead.
但是请注意,更推荐使用Vahe Shadunts 的答案,它使用了header()。
回答by Fabian Schmengler
The refreshheader does the job but I'd like to highlight some potential issues:
该refresh头做这项工作,但我想强调一些潜在的问题:
- It is not specified in the HTTP standard. Wikipedia says: - Proprietary and non-standard: a header extension introduced by Netscape and supported by most web browsers. - But it has been around for almost 20 years now and I don't know of any browser that does notsupport it (could not find a reference though) 
- Some browsers do not use the cache on a page redirected with - refresh. It has been demonstrated for Internet Explorer here: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/meta-refresh-causes-additional-http-requests.aspxand I coud reproduce it on Firefox. Chrome does not have this issue.
- 它没有在 HTTP 标准中指定。维基百科说: - 专有和非标准:由 Netscape 引入并被大多数 Web 浏览器支持的标头扩展。 - 但它已经存在近 20 年了,我不知道有任何不支持它的浏览器(虽然找不到参考) 
- 某些浏览器不使用重定向的页面上的缓存 - refresh。它已在此处为 Internet Explorer 演示:http: //blogs.msdn.com/b/ieinternals/archive/2010/05/13/meta-refresh-causes-additional-http-requests.aspx,我可以在火狐。Chrome 没有这个问题。
Alternative: JavaScript
替代方案:JavaScript
You can add a JavaScript on the intermediate page, that opens a new page after X seconds. Add this at the bottom of the page to redirect to http://www.example.com/targetafter 5seconds:
您可以在中间页面上添加一个 JavaScript,它会在 X 秒后打开一个新页面。在页面底部添加此内容以http://www.example.com/target在5几秒钟后重定向到:
<script type="text/javascript">
    window.setTimeout(function() {
        window.location.href='http://www.example.com/target';
    }, 5000);
</script>
Combination
组合
As a bonus, you can fall back to the refreshheader if JS is disabled, using the metadirective http-equivthat tells the browser to act as if a certain HTTP header has been sent. Because it is part of the HTML source, you can wrap it in a <noscript>element. Add this to your <head>additionally to the JavaScript above:
作为奖励,refresh如果 JS 被禁用,您可以回退到标头,使用meta指令http-equiv告诉浏览器好像已经发送了某个 HTTP 标头一样。因为它是 HTML 源代码的一部分,所以您可以将它包装在一个<noscript>元素中。<head>在上面的 JavaScript 中添加这个:
<noscript>
    <meta http-equiv="refresh" content="5;url=http://www.example.com/target" />
</noscript>
Now, the page redirects with JavaScript if available for the best performance, and uses refreshotherwise.
现在,页面会使用 JavaScript 重定向以获得最佳性能,refresh否则会使用其他方式。

