views:

2275

answers:

5
+1  Q: 

PHP Redirect Pause

How do I pause a page for a certain amount of seconds before redirecting the user to another HTML/PHP page using PHP?

+1  A: 

Update: this works, but may not be the most appropriate solution for this situation. See comments.

Might this be what you are looking for?

<?php
sleep(5);
header("Location: http://www.example.com/");
exit();
?>
Daan
Does this really work?I thought that with the sleep function the browser just keep'n'load the page for (in your example) 5 seconds.. but without throw out the output
DaNieL
@DaNieL I haven't tested this, so you may be correct that it does or does not work depending on server settings or the time / place that you call this code.
Daan
I've just tested, it works, but prevent all the others output too.. afaik the sleep sleep() funcion keep in pause the entire php render, so imho shouldn't be used for this kind of issues
DaNieL
You should put a call to flush() before sleep() to send whatever has been output to the browser.
Ollie Saunders
+2  A: 

The other 2 options are a Javascript redirect using setTimeout() or a meta refresh tag with a timeout.

Darryl Hein
+9  A: 

Low-tech solution requiring no Javascript or even PHP:

<html>
<head>
    <title>Redirecting...</title>
    <meta http-equiv="refresh" 
content="10;URL=http://www.newsite.com"&gt;
</head>
<body>
    You are being automatically redirected to a new location.<br />
    If your browser does not redirect you in 10 seconds, or you do
    not wish to wait, <a href="http://www.newsite.com"&gt;click here</a>. 
</body>
</html>

The advantage of this solution over using the "Location:" header is that you don't need to pause the script execution, which will appear to the user as if the server is busy or their connection has hung.

This solution also gives the user the option of proceeding to the new page immediately rather than having to wait x number of seconds while their browser displays no information.

Edit: I think it's also worth noting that if you do decide to use the the header() method, you need to make sure your sleep() duration isn't too long. I think most browsers generate a connection timed out after not receiving any data from the server for 1 minute.

Calvin
Be carefully - some browser (like chrome and opera) won't take this way correctly
DaNieL
Really? I did not know that. Well, at least they have the link to click on then.
Calvin
Yes, i dont remember exacly if the browser who didnt handle it properly are safari, opera or chrome.. i remember that as 2 of them.... but maybe they fixed this issue in the time
DaNieL
Wait, all! Just tested this way with: ff, safari, chrome, opera and IE.. all of them handle it correctly.I'm sure that just une years ago two of them didn't, but now seem resolved.
DaNieL
+8  A: 

This one should works:

<?php
header('Refresh: 5; URL=http://yoursite.com/page.php');
//other code
?>

and will allow your user to see whatever kind of output you want (You'll be redirected in X Seconds, click yere if dont, etc..)

DaNieL
I actually like this solution much better than what I posted. +1
Calvin
This doesn’t work with every browser.
Gumbo
really? i tested on chrome, ff, ie, opera and safari, all on windows environment, and all works as the same.On what OS did you try it?Couldnt be any kind of browser's setting?
DaNieL
+4  A: 

never use sleep this way. Even under slight load your server will run out of http connections. Worst if your firewall runs out.

The delay parameter in the redirect header is made for exactly to the same reason.

Csaba Kétszeri