【问题标题】:how to switch to another page in 3 seconds in PHP?如何在 PHP 中在 3 秒内切换到另一个页面?
【发布时间】:2009-06-30 20:05:46
【问题描述】:

这是我的尝试:

@header("Content-type: text/html; charset=utf-8");
@header("Location:/index.php");
@header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
@header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

如你所见,“3秒”是无法控制的,如何让它在3秒内生效?

【问题讨论】:

    标签: php refresh


    【解决方案1】:

    最简单的方法是使用meta redirect 或javascript 重定向。说到你如何提供文本/html,你可以将其中任何一个回显到浏览器。

    <meta http-equiv="refresh" content="3;URL=/newpage.php">
    

    window.setTimeout(function() { window.location = '/newpage.php' }, 3000);
    

    编辑:根据URL redirection 上的维基百科页面,您可以将Refresh 标头从PHP 直接发送到浏览器。不知道浏览器对此的支持程度如何。

    header('Refresh: 3; url=/newpage.php');
    

    【讨论】:

    • 我记得看到一个 PHP 函数可以同时输出两者。真的不需要去追捕它 - 提问者可以实现它。
    【解决方案2】:

    这在 PHP 中应该可以工作:

    header('Refresh: 3; url=index.php');
    

    【讨论】:

    • 不同浏览器如何支持?
    • AFAIK,所有主流浏览器都支持。
    • 请注意,此代码必须位于页面上任何其他 php 代码之前。
    【解决方案3】:

    您可以利用刷新元标记,如下所示:

    <html> 
    <head> 
        <title>redirect page</title> 
        <META http-equiv="refresh" content="5;URL=http://www.newurl.com"> 
    </head> 
    <body bgcolor="#ffffff"> 
        The contents you are looking for have moved. 
        You will be redirected to the new location automatically in 5 seconds.
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2012-07-18
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 1970-01-01
      • 2022-01-10
      相关资源
      最近更新 更多