一定时间后页面重定向 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6119451/
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
Page redirect after certain time PHP
提问by afaolek
There is a certain PHP function for redirecting after some time. I saw it somewhere but can't remember. It's like the gmail redirection after logging in. Please, could anyone remind me?
有一个特定的 PHP 函数可以在一段时间后进行重定向。我在哪儿见过,但想不起来了。就像登录后的gmail重定向一样。拜托,有人能提醒我吗?
回答by Teneff
header( "refresh:5;url=wherever.php" );
this is the php way to set header
which will redirect you to wherever.php
in 5 seconds
这是 php 设置方式header
,它将wherever.php
在5 秒内将您重定向到
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file. (source php.net)
请记住,header() 必须在发送任何实际输出之前调用,无论是通过普通的 HTML 标记、文件中的空行还是来自 PHP。使用 include 或 require 函数或其他文件访问函数读取代码,并且在调用 header() 之前输出空格或空行,这是一个非常常见的错误。使用单个 PHP/HTML 文件时存在同样的问题。(来源php.net)
回答by Ibu
You can use javascript to redirect after some time
您可以在一段时间后使用 javascript 重定向
setTimeout(function () {
window.location.href= 'http://www.google.com'; // the redirect goes here
},5000); // 5 seconds
回答by royrui
You can try this:
你可以试试这个:
header('Refresh: 10; URL=http://yoursite.com/page.php');
Where 10 is in seconds.
其中 10 以秒为单位。
回答by John
you would want to use php to write out a meta tag.
你会想用 php 写出一个元标记。
<meta http-equiv="refresh" content="5;url=http://www.yoursite.com">
It is not recommended but it is possible. The 5 in this example is the number of seconds before it refreshes.
不推荐,但有可能。本例中的 5 是刷新前的秒数。
回答by JJHyman
header( "refresh:5;url=wherever.php" );
indeed you can use this code as teneff said, but you don't have to necessarily put the header before any sent output (this would output a "cannot relocate header.... :3 error").
实际上,您可以像teneff 所说的那样使用此代码,但您不必将标头放在任何发送的输出之前(这将输出“无法重新定位标头....:3 错误”)。
To solve this use the php function ob_start();
before any html is outputed.
要解决此问题,请ob_start();
在输出任何 html 之前使用 php 函数。
To terminate the ob just put ob_end_flush();
after you don't have any html output.
在ob_end_flush();
没有任何 html 输出后终止 ob 。
cheers!
干杯!
回答by JustJohn
The PHP refresh after 5 seconds didn't work for me when opening a Save As dialogue to save a file: (header('Content-type: text/plain'); header("Content-Disposition: attachment; filename=$filename>");)
打开“另存为”对话框以保存文件时,5 秒后的 PHP 刷新对我不起作用:(header('Content-type: text/plain'); header("Content-Disposition: attachment; filename=$filename >");)
After the Save As link was clicked, and file was saved, the timed refresh stopped on the calling page.
单击“另存为”链接并保存文件后,定时刷新停止在调用页面上。
However, thank you very much, ibu's javascript solution just kept on ticking and refreshing my webpage, which is what I needed for my specific application. So thank you ibu for posting javascript solution to php problem here.
但是,非常感谢您,ibu 的 javascript 解决方案只是不断地打勾和刷新我的网页,这正是我的特定应用程序所需要的。所以感谢 ibu 在这里发布针对 php 问题的 javascript 解决方案。
You can use javascript to redirect after some time
您可以在一段时间后使用 javascript 重定向
setTimeout(function () {
window.location.href = 'http://www.google.com';
},5000); // 5 seconds
回答by Prodip Kirtania
You can use this javascript code to redirect after a specific time. Hope it will work.
您可以使用此 javascript 代码在特定时间后重定向。希望它会起作用。
setRedirectTime(function ()
{
window.location.href= 'https://www.google.com'; // the redirect URL will be here
},10000); // 10 seconds
回答by Jason Palmer
If you are redirecting with PHP, then you would simply use the sleep() command to sleep for however many seconds before redirecting.
如果您使用 PHP 进行重定向,那么您只需使用 sleep() 命令在重定向前休眠几秒钟。
But, I think what you are referring to is the meta refresh tag:
但是,我认为您所指的是元刷新标签:
http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm
http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm