如何在几秒钟后在没有元的情况下在 PHP 中重定向页面 http-equiv=REFRESH CONTENT=time
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11154523/
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
How to Redirect Page in PHP after a few seconds without meta http-equiv=REFRESH CONTENT=time
提问by zhenming
It seems that it is not advisable to use
似乎不建议使用
<meta http-equiv=REFRESH CONTENT=3;url=url>
for redirects but instead use
用于重定向,而是使用
header('Location: url')
However, I would like to show the user some message and allow them some time to read it before redirecting. Is there a way to do it without meta?
但是,我想向用户显示一些消息并让他们在重定向之前有时间阅读它。有没有办法在没有元的情况下做到这一点?
回答by Dador
Try use "refresh" header:
尝试使用“刷新”标题:
header('Refresh: 3;url=page.php');
Also, you can look at this Question Refresh HTTP Header.
此外,您可以查看此问题刷新 HTTP 标头。
回答by Brandon Wamboldt
There is nothing wrong with using the meta refresh tag.
使用元刷新标签没有任何问题。
<meta http-equiv="refresh" content="5;URL='http://example.com/'" />
That tag says wait 5 seconds and redirect to example.com. This tag isn't a problem unless users are on IE6 and it still works, just breaks the history buttons.
该标签表示等待 5 秒并重定向到 example.com。这个标签不是问题,除非用户使用 IE6 并且它仍然有效,只是打破了历史按钮。
Using JavaScript is an option, but make sure you include a link saying"If you are not automatically redirected, please click here". You should actually include that link either way.
使用 JavaScript 是一种选择,但请确保包含一个链接,上面写着“如果您没有自动重定向,请单击此处”。无论哪种方式,您实际上都应该包含该链接。
回答by Sanjay
php way to set header, will redirect you to test.php in 5 seconds:
php 设置标题的方式,将在 5 秒内将您重定向到 test.php:
header( "refresh:5;url=test.php" );
call before any actual output is sent.
在发送任何实际输出之前调用。
And in javascript:
在 javascript 中:
setTimeout(function () {
window.location.href= url; // the redirect goes here
},5000); // 5 seconds
回答by Death
Header tags are sent on page load, to the browser, so that it can quickly redirect the user to the desired page without bothering to render it or even load it into the history. As such, you can't call a redirect once the page has already loaded, as the headers have already been dealt with.
标题标签在页面加载时发送到浏览器,以便它可以快速将用户重定向到所需的页面,而无需费心渲染它甚至将其加载到历史记录中。因此,一旦页面已经加载,您就不能调用重定向,因为标题已经被处理。
You can instead perform this with:
您可以改为执行此操作:
header( "refresh:5;url=wherever.php" );
Which basically sets the <meta>tag in the headers of the page itself, meaning you don't need to write the tag out.
这基本上<meta>是在页面本身的标题中设置标签,这意味着您不需要写出标签。
回答by Patrick K.
By what you guys are saying, theoretically this should work then:
根据你们所说的,理论上这应该有效:
URL: http://www.example.com/ticketgen/index.php?success=1&redir=1
网址:http: //www.example.com/ticketgen/index.php?success=1& redir=1
<?php
$myredir = ($_GET['redir']);
if ($myredir == 1)
{
header( "refresh:5;url=http://www.example.com/ticketgen/" );
}
?>
But it does nothing. I also have it at the VERY TOP of the page so it can send the headers.
但它什么也不做。我也把它放在页面的顶部,所以它可以发送标题。
it doesn't work in Firefox i just found out.
我刚刚发现它在 Firefox 中不起作用。
回答by user2851654
I think really the best way is header("Refresh: 10;url=../index.php");Like what i've done with my work.
我认为最好的方法是header("Refresh: 10;url=../index.php");就像我对我的工作所做的那样。
回答by Jason Larke
You can do it with a small piece of javascript:
你可以用一小段 javascript 来完成:
<script type="text/javascript" language="JavaScript">location.href = 'otherpage.php';</script>
Of course, this will depend on the person having JavaScript enabled.
当然,这取决于启用 JavaScript 的人。
Obviously, to set the delay you can use something like setTimeout:
显然,要设置延迟,您可以使用类似 setTimeout 的方法:
<script type="text/javascript" language="JavaScript">
setTimeout(function () {
location.href = 'stackoverflowhelp.php';
}, 5000);
</script>
回答by Rajeshdn
https://codingislove.com/redirect-pages-php/
https://codingislove.com/redirect-pages-php/
check out the above article, where they clearly explained about how to redirect the pages in PHP by setting time.
查看上面的文章,他们清楚地解释了如何通过设置时间在 PHP 中重定向页面。
Redirecting code without time set:header('location:URL ADDRESS');
没有时间设置的重定向代码:header('location:URL ADDRESS');
Redirecting code with three seconds time set:header('refresh:3; url=URL ADDRESS');
使用三秒时间设置重定向代码:header('refresh:3; url=URL ADDRESS');

