tags:

views:

300

answers:

7

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

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

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

Thanks

+10  A: 

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

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"&gt;

or javascript.

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


Found in the comments from Daniel:

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

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

Jonathan Sampson
For the sake of completeness, it's probably worth mentioning that user agents are free to ignore the information in a refresh tag... and, of course, not all users agents support (or enable) JavaScript. The refresh tag will probably work in more user agents, so if you really, really think you need some kind of delayed redirect, I vote for that.
Jan Krüger
Thank you for the contribution, Jan. Much appreciated.
Jonathan Sampson
never use sleep() for this kind of things!!
DaNieL
almost forgot, there is even the header solution: `header('Refresh: 10; URL=http://yoursite.com/page.php');`
DaNieL
The header would be the ideal solution, as it doesn't require any Javascript or Meta Tag coding.
Chacha102
Added that to the answer. You can thank me for the extra upvotes you'll be getting Johnathan :)
Chacha102
A: 

You'll want to do a client side redirect:

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

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

<?php

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

?>
gunderson
+2  A: 

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.

FractalizeR
+1  A: 

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.

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

Dolbz
A: 

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.

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

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 http-equiv="refresh" content="10;url=http://example.com"/&gt;

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

Chacha102
A: 

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:

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

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

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

imran