php php脚本在某个页面停留几秒钟并重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3768573/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:01:56  来源:igfitidea点击:

php script to stay for few second in some page and redirect

php

提问by ktm

Is there a way to make page display few seconds in php and redirect to another page ?

有没有办法让页面在 php 中显示几秒钟并重定向到另一个页面?

回答by mkoistinen

The meta redirect is probably what you want, but you CANdo this in PHP too, like this:

元重定向可能是你想要什么,而是你CAN做到这一点在PHP太像这样:

<?php header("Refresh: 10;url=http://www.yourdestination.com/"); ?>

Where 10 is the number of seconds to wait.

其中 10 是等待的秒数。

回答by Gordon

EDITOk, I stand corrected. Corrected answer below.

编辑好的,我已经纠正了。下面更正答案。

You caneither use PHP's headerfunction as shown elsewhere on this page.

可以使用header本页其他地方所示的PHP函数。

If you want to do a refresh after the page is rendered, you can do so with JavaScript or a Meta Refresh. For users that block meta refreshs and have JavaScript disabled it is good practise to provide a link that can be clicked manually to get to the new target.

如果您想在页面呈现后进行刷新,可以使用 JavaScript 或 Meta Refresh 来完成。对于阻止元刷新并禁用 JavaScript 的用户,最好提供一个可以手动单击以到达新目标的链接。

Example:

例子:

<?php header("Refresh: 2;url=http://www.example.com/"); ?>
<html>
    <head>
        <title>Redirects</title>
        <meta http-equiv="refresh" content="2; URL=http://example.com" />
        <script type="text/javascript">
                    window.setTimeout(function() {
                        location.href = 'http://example.com';
                    }, 2000);
        </script>
    </head>
    <body>
        <p>Click here if you are not redirected automatically in 2 seconds<br />
            <a href="http://example.com">Example.com</a>.
        </p>
    </body>
</html>

Please also see the WCAG suggestions about Automatic Page Refreshes.

另请参阅有关自动页面刷新WCAG 建议

回答by Alex

However, you're probably best off doing this in JavaScript

但是,您可能最好在 JavaScript 中执行此操作

setTimeout(function()
{

   window.location = "http://www.somedomain.com/somepage.php";

}, 5000); // 5 seconds

See @Gordon's answer a above for a more user-friendly and complete example, this is merely one method.

有关更加用户友好和完整的示例,请参阅上面@Gordon 的回答 a,这只是一种方法。

回答by fabrik

With META redirect you can:

使用 META 重定向,您可以:

<meta http-equiv="refresh" content="2;url=http://example.com/">

<meta http-equiv="refresh" content="2;url=http://example.com/">

Where 2is the delay in seconds.

2以秒为单位的延迟在哪里。

回答by Knowledge Craving

Use the following code in PHP, but only after understanding this manual pagefully (this is the main important part when using the following code):-

在 PHP 中使用以下代码,但只有在完全理解本手册页后(这是使用以下代码时的主要重要部分):-

$redirectionTime = 5;
$newPageUrl = "wherever_page.php";
header( "Refresh: $redirectionTime; url=$newPageUrl" );
echo "You will now be redirected to a new page, after $redirectionTime seconds. Please be patient...";
exit();

The above code will redirect the user to the "wherever_page.php" page from the existing page after exactly 5 seconds. But you need to do another important thing.

上面的代码将在 5 秒后将用户从现有页面重定向到“wherever_page.php”页面。但是你需要做另一件重要的事情。

You need to start the Output Buffer first, so that in case you output any HTML before calling the "header()" function, no warning or fatal error will be given. In order to do this, you need to call the following function at the very first line of your web page, whether you include anything or not:-

您需要先启动输出缓冲区,以便万一您在调用 " header()" 函数之前输出任何 HTML ,都不会给出警告或致命错误。为此,无论是否包含任何内容,您都需要在网页的第一行调用以下函数:-

<?php
ob_start();
// Rest of the web page logic comes after this

The main advantage of the above sets of code is that even if the JavaScript is disabled for that browser, the redirection will still occur.

上述代码集的主要优点是,即使该浏览器禁用了 JavaScript,重定向仍然会发生。

Hope it helps.

希望能帮助到你。