在 PHP 中使用计时器重定向?

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

Redirect with Timer in PHP?

php

提问by imran

How can I make a redirect with PHP after say 10 seconds...

如何在 10 秒后使用 PHP 进行重定向...

I have read alot about it, seems like it would be better with javascript. But PHP would save me alot of coding.

我已经阅读了很多关于它的内容,似乎使用 javascript 会更好。但是 PHP 会为我节省很多编码。

So how can I make a redirect with timer in PHP ?

那么如何在 PHP 中使用计时器进行重定向?

Thanks

谢谢

回答by Sampson

You can cause your PHP script to sleep for 10 seconds,

你可以让你的 PHP 脚本休眠 10 秒,

sleep(10);

but this will appear to the end-user as a non-responsive server. The best option is to use either a meta refresh,

但这在最终用户看来将是一个无响应的服务器。最好的选择是使用元刷新,

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

or javascript.

或javascript。

setTimeout(function(){
  window.location = "http://google.com";
}, 10000);


Found in the comments from Daniel:

在 Daniel 的评论中找到:

header('Refresh: 10; URL=http://yoursite.com/page.php');

would be ideal for this situation, as it requires no Javascript or HTML.

将是这种情况的理想选择,因为它不需要 Javascript 或 HTML。

回答by imran

header('Refresh: 10; URL=http://yoursite.com/page.php');

header('刷新:10; URL=http://yoursite.com/page.php');

Place this PHP code inside header section of the page, otherwise, it wouldn't work.

将此 PHP 代码放在页面的标题部分,否则将无法工作。

回答by steve

<?php

      header( "refresh:10; url=https://example.com/index.php" );

?>

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redirect with countdown</title>

<style type="text/css">
.myClass {
           font-family: verdana; 
           font-size: 16px; 
           font-weight: normal;  
           color: black;
           line-height: 30px;
         }
</style>
</head>
<body>
<script type="text/javascript">

 (function () {
  var timeLeft = 10,
  cinterval;

  var timeDec = function (){
  timeLeft--;
  document.getElementById('countdown').innerHTML = timeLeft;
  if(timeLeft === 0){
  clearInterval(cinterval);
    }
};

cinterval = setInterval(timeDec, 1000);
})();

</script>
Redirecting in <span id="countdown">10</span> seconds.
<br><br>
<span class="myClass">Thank you! Your submission has been received!</span>
</body>
</html>

回答by gunderson

You'll want to do a client side redirect:

您需要进行客户端重定向:

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

but if you feel like it has to be in PHP, you could do something like:

但是如果您觉得它必须在 PHP 中,您可以执行以下操作:

<?php

// wait 5 seconds and redirect :)
echo "<meta http-equiv=\"refresh\" content=\"5;url=http://yourdomain.com\"/>";

?>

回答by Vladislav Rastrusny

That is a bad idea to make PHP script sleeping. Actually it is a way to DoS your server easily ;) PHP script in memory is consuming enough resources especially if it is working as CGI.

让 PHP 脚本休眠是个坏主意。实际上,这是一种轻松 DoS 服务器的方法;) 内存中的 PHP 脚本消耗了足够的资源,尤其是当它作为 CGI 工作时。

回答by Dolbz

PHP is the probably wrong technology for this as it runs server-side and won't provide useful feedback to the user during the delay.

PHP 可能是错误的技术,因为它在服务器端运行,并且不会在延迟期间向用户提供有用的反馈。

This isn't a lot of coding in Javascript. See this linkfor a very easy way to implement this on your page

这不是很多 Javascript 编码。有关在您的页面上实施此操作的非常简单的方法,请参阅此链接

回答by Tyler Carter

After the Web Page loads, PHP no longer runs. This means that you can't do anything with PHP after the page loads unless you use something like AJAX(Javascript calling a PHP page) to transfer data to the page. This presents you with a few methods to achieve your desired 10 second wait on redirect.

网页加载后,PHP 不再运行。这意味着在页面加载后你不能用 PHP 做任何事情,除非你使用像 AJAX(Javascript 调用 PHP 页面)之类的东西将数据传输到页面。这为您提供了一些方法来实现所需的 10 秒重定向等待时间。

First, you could tell your script to sleep() for 10 seconds. This however, as Johnathan mentioned, means that you would look like your page was really slow, only to have the user redirected.

首先,您可以告诉脚本 sleep() 10 秒钟。然而,正如 Johnathan 所提到的,这意味着您的页面看起来真的很慢,只是让用户重定向。

sleep(10);

You could also simply drop in a META tag that tells the page to redirect itself after 10 seconds. This is the preferred method, as it doesn't involved almost any other coding, as you simply drop in the META tag, and you don't have to deal with javascript at all.

您也可以简单地放入一个 META 标记,该标记告诉页面在 10 秒后自行重定向。这是首选方法,因为它几乎不涉及任何其他编码,因为您只需放入 META 标记,而您根本不必处理 javascript。

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

Then, you could also have Javascript issue a location.href="bleh"; command after waiting for 10 seconds.

然后,您也可以让 Javascript 发出 location.href="bleh"; 等待 10 秒后发出命令。

回答by Corvin

Another method worth mentioning is sending a Location HTTP header with PHP's header()function. So, if you want a robust solution that avoids javascript and meta tags, while keeping web application responsive, that might be creating an iframe (or a frame) and loading there a php script that contains the following:

另一个值得一提的方法是使用 PHP 的header()函数发送 Location HTTP 标。因此,如果您想要一个避免 javascript 和元标记的强大解决方案,同时保持 Web 应用程序的响应性,那可能是创建一个 iframe(或一个框架)并在那里加载一个包含以下内容的 php 脚本:

sleep(10);
header("Location: http://my.redirect.location.com");

回答by micaball

I managed to do it with a simple function on PHP

我设法用 PHP 上的一个简单函数做到了

function RedirectToURL($url, $waitmsg = 0.4)
{
    header("Refresh:$waitmsg; URL= $url");
    exit;
}

and you call from your PHP for waiting 2 seconds before redirect. Note that $waitmsg = 0.4 is used for defining the default value.

并且您在重定向前从 PHP 调用等待 2 秒。请注意,$waitmsg = 0.4 用于定义默认值。

RedirectToURL("http://website.php", 2);

If you want to redirect after a form submit, you can try

如果您想在表单提交后重定向,您可以尝试

if(isset($_POST['submitted']))
{
       echo '<div style="text-align:center; margin-left:auto; margin-right:auto"><br><br>Sucess. Thanks for contribution.</div>';
       RedirectToURL("http://thewebsiteilookforaftersubmit", 2); // I want to wait 2 seconds and not 0.4 as defined by default
}

回答by ElenaRuff

Create a file, for example: "go.php", with code

创建一个文件,例如:“go.php”,带有代码

<?php
$site = $_GET['iam'];
sleep(5);
Header ("Location: ".$site."");
exit();
?>

<a href="http://whatever.com/go.php?iam=http://google.com">Link</a>

or like this, without "sleep"

或者像这样,没有“睡眠”

<?php
$site = $_GET['iam'];
Header('HTTP/1.1 301 Moved Permanently');
Header("Refresh: 10; URL=".$site."");
exit();
?>

<a href="http://whatever.com/go.php?iam=http://google.com">Link</a>