111

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?

afaolek
  • 8,452
  • 13
  • 45
  • 60
  • @zerkms: Looks like there are a couple ways to do it. – Wesley Murch May 25 '11 at 04:41
  • 2
    @Wesley Murch: `Refresh` header is not a standart one. I'd never use things that are not covered with RFCs – zerkms May 25 '11 at 05:58
  • http://stackoverflow.com/questions/18305258/display-message-before-redirect-to-other-page#comment35908148_18305289 led me to http://stackoverflow.com/questions/283752/refresh-http-header which says that as well as not being standard the refresh header also causes performance issues in Internet Explorer. – Edward Apr 06 '16 at 18:59

9 Answers9

263
header( "refresh:5;url=wherever.php" );

this is the php way to set header which will redirect you to wherever.php in 5 seconds


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)

Teneff
  • 30,564
  • 13
  • 72
  • 103
  • 1
    interesting i never seen this method before... but isn't that going to display a blank page or just idle without any message until the timer runs out? – Ibu May 25 '11 at 04:25
  • it is going to display the page ... all that it does is to set header witch will tell the browser to refresh the page in 5 seconds, if you really want to display blank page simply use `die();` – Teneff May 25 '11 at 04:27
  • 2
    The bad thing about this is: This header is not in the HTTP standard, clients are free to ignore it. – Sven Apr 21 '13 at 14:35
  • :) Good idea! And if you want to send (keep) some variables and use in the reloaded page? – Mugur Ungureanu May 23 '14 at 08:16
  • its maybe a billion years since this post but how do you post a message while redirection like maybe "Redirecting page, please wait" – CAO Feb 02 '15 at 18:47
  • @CAO To display a custom message before redirecting to other page, just echo the message at the bottom of the above header call. So the message will be displayed for 5 seconds and then the page will be redirected. – shasi kanth Apr 07 '15 at 10:06
  • I saw somewhere that after sending a header it is better to use `die` to avoid conflict....but if I use `die` in this case I wouldn't output anything, so what can I do about that? or using `die` is not necessary if I simply output sth in the page and redirect after x seconds? – Andrew Sep 06 '15 at 19:11
  • http://stackoverflow.com/questions/18305258/display-message-before-redirect-to-other-page#comment35908148_18305289 led me to http://stackoverflow.com/questions/283752/refresh-http-header which says that as well as not being standard the refresh header also causes performance issues in Internet Explorer. – Edward Apr 06 '16 at 19:02
  • you can echo whatever message you want to show before the `header()` and then use `die()`. This way the message will be printed and no further conflict will occur. – Dev Agrawal May 04 '17 at 07:18
27

You can use javascript to redirect after some time

setTimeout(function () {
   window.location.href= 'http://www.google.com'; // the redirect goes here

},5000); // 5 seconds
Ibu
  • 42,752
  • 13
  • 76
  • 103
  • 5
    @Jami it is true, that is why i said the OP can use javascript instead. – Ibu May 14 '13 at 16:27
  • @BenjaminIntal This also works as a great fallback if the browser ignores a redirection in the header. You'd have to use a browser that does not accept the header AND have Javascript disabled to not receive the redirection. – Super Cat Feb 04 '17 at 05:28
18

You can try this:

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

Where 10 is in seconds.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
royrui
  • 1,217
  • 10
  • 20
12

you would want to use php to write out a meta tag.

<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.

John
  • 1,530
  • 1
  • 10
  • 19
4
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").

To solve this use the php function ob_start(); before any html is outputed.

To terminate the ob just put ob_end_flush(); after you don't have any html output.

cheers!

Jefferson
  • 794
  • 10
  • 24
JJJack
  • 61
  • 1
  • 1
  • 4
4

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>");)

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.

You can use javascript to redirect after some time

setTimeout(function () {    
    window.location.href = 'http://www.google.com'; 
},5000); // 5 seconds
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
JustJohn
  • 1,362
  • 2
  • 22
  • 44
3

You can use this javascript code to redirect after a specific time. Hope it will work.

setRedirectTime(function () 
{
   window.location.href= 'https://www.google.com'; // the redirect URL will be here

},10000); // 10 seconds
Prodip Kirtania
  • 99
  • 1
  • 11
3

If you are redirecting with PHP, then you would simply use the sleep() command to sleep for however many seconds before redirecting.

But, I think what you are referring to is the meta refresh tag:

http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm

Jason Palmer
  • 731
  • 4
  • 17
3

Redirect PHP time programming:

 <?php
 header("Refresh:10;url=***-----índex.php--OR----URL-----");
 ?>
Paulo Boaventura
  • 1,365
  • 1
  • 9
  • 29