Is there a way to make page display few seconds in php and redirect to another page ?
-
4Not in php (as that is only server-sided), but redirects are pretty common. – Wrikken Sep 22 '10 at 11:05
-
1That's ugliest way to show a message. Do not use it. – Your Common Sense Sep 22 '10 at 11:26
-
2@mkoistinen: I stand corrected – Wrikken Sep 22 '10 at 11:39
-
Thank you so much mkoistinen. Thanks for the help – ktm Sep 22 '10 at 18:30
5 Answers
The meta redirect is probably what you want, but you CAN do this in PHP too, like this:
<?php header("Refresh: 10;url=http://www.yourdestination.com/"); ?>
Where 10 is the number of seconds to wait.

- 312,688
- 75
- 539
- 559

- 7,724
- 3
- 41
- 56
-
@Gordan, there is no reason why you can't emit HTML after your headers. This HTML would be shown just fine, then, 10 seconds later, you'll be redirected. I've been doing this in PHP since 1998, please check you facts before down-voting someone. – mkoistinen Sep 22 '10 at 11:26
-
There is a solution for the output also. Please the following link for more details about "Remember that header() must be called before any actual output is sent" http://stackoverflow.com/questions/3768573/php-script-to-stay-for-few-second-in-some-page-and-redirect/3768629#3768629. – Knowledge Craving Sep 22 '10 at 11:29
-
Please try to look what the "ob_start()" function does, & then provide your valuable comments. I'm using it for the past 3 years without any issues at all. – Knowledge Craving Sep 22 '10 at 11:32
-
3@Col. Shrapnel, I am only providing the answer *specifically requested by the OP*, I didn't pass any judgement on the question. – mkoistinen Sep 22 '10 at 11:35
-
3@Col. Shrapnel, are you going for the "lounge-chair commenter" badge? I didn't see you make one suggestion for the OP. You've only criticised everyone else's suggestions. – mkoistinen Sep 22 '10 at 11:40
-
1Ah, it stands to reason that a meta with an `http-equiv` would have a normal HTTP header family member, just never took the time to think about it ;) – Wrikken Sep 22 '10 at 11:43
-
1@mkoistinen that's what Shrapnel usually does :) And you are indeed right about `header` working and hence removed the downvote. Sorry for the inconvenience and thanks for making me learn something new. – Gordon Sep 22 '10 at 11:44
-
@Col. Shrapnel - thanks for your advise & making me more perfect in thinking why **full knowledge is more important than little knowledge of something**. – Knowledge Craving Sep 22 '10 at 11:44
-
1@Col. Shrapnel : what is the exact ugly bit? No javascript needed, no HTML clutter, I'd even go as far as to say it's an _elegant_ solution to the problem, although I disagree with the practice of redirecting to another page. – Wrikken Sep 22 '10 at 11:47
-
1@Wrikken, I think CS's main objection is flashing a page in front of someone then taking them somewhere else. However, this is a common technique to display a thank you or some other message before beginning a download for the user. If the redirect destination is a download, then it will occur without disrupting the current display. Still very useful today. – mkoistinen Sep 22 '10 at 11:49
-
1
-
1@Wrikken 1. it is used often after POST, instead of standard GET redirect, and produces a "page cannot be displayed" error when user press Back. That is driving me crazy when I try to use old vBulletin forum or any other site using this ugly method, thanks God, number of them constantly decreasing. 2. Method itself is driving me crazy too, as it shows me useless stupid message instead of bringing me to the right place already. If it's important message, DO NOT leave this page. If it's just useless info "you will be redirected" - DO NOT show this page at all. That's it – Your Common Sense Sep 22 '10 at 11:55
-
1@Col. Shrapnel, all fair points, but as I mentioned in my reply to @Wrikken above, there are still great uses for this. – mkoistinen Sep 22 '10 at 12:02
-
I'm understand it works still looks weird for me. (And it can cause problems too: http://stackoverflow.com/questions/1884365/php-headerrefresh-problems) – fabrik Sep 22 '10 at 12:56
-
1@fabrik, did you just down-vote me? It is specifically the solution the OP asked for **specifically**. Unbelievable. – mkoistinen Sep 22 '10 at 12:59
-
No one query it's works but still a **bad** practice (look yourself in Google) that shouldn't be advert. – fabrik Sep 22 '10 at 13:02
-
@fabrik Which is specifically why I put "The meta redirect is probably what you want..." in my post, or did you bother to read it? – mkoistinen Sep 22 '10 at 13:06
-
Dude, you're the only one who care about rep points. Call your friends too let my anser go as deep as you downvote it. – fabrik Sep 22 '10 at 13:17
-
1@Fabrik, not at all, but I am alarmed that people would be so tactical on this site when the intention is to help people. Also, at this point, nearly 21% of your votes are down-votes. Only 5% of mine are. So... who is the one that cares about Rep? – mkoistinen Sep 22 '10 at 13:21
-
1@mkoistinen careful, if the amount of a user's downvotes were a measure, one could argue you are just too stingy too downvote because it substracts from your reputation ;) – Gordon Sep 22 '10 at 13:34
-
Man, please think again what you said: 21% downvotes against 5% means i have four times more minus than you. – fabrik Sep 22 '10 at 13:35
-
@Gordon: that was something i can't define because my poor english. thank you. – fabrik Sep 22 '10 at 13:36
-
3@fabrik cheers, but I basically agree with Martin that the downvote is unjustified. It answers the OP's question and it was the first to provide a PHP solution while the other answers (mine included) said it's not possible to do it with PHP (which, in retrospective, still leaves me wondering what made me think that). – Gordon Sep 22 '10 at 13:46
EDIT Ok, I stand corrected. Corrected answer below.
You can either use PHP's header
function as shown elsewhere on this page.
If you want to do a refresh after the page is rendered, you can do so with JavaScript or a Meta Refresh. For users that block meta refreshs and have JavaScript disabled it is good practise to provide a link that can be clicked manually to get to the new target.
Example:
<?php header("Refresh: 2;url=http://www.example.com/"); ?>
<html>
<head>
<title>Redirects</title>
<meta http-equiv="refresh" content="2; URL=http://example.com" />
<script type="text/javascript">
window.setTimeout(function() {
location.href = 'http://example.com';
}, 2000);
</script>
</head>
<body>
<p>Click here if you are not redirected automatically in 2 seconds<br />
<a href="http://example.com">Example.com</a>.
</p>
</body>
</html>
Please also see the WCAG suggestions about Automatic Page Refreshes.

- 312,688
- 75
- 539
- 559
-
@Gordan, I've been doing this since 1998 with PHP. Have you even tried it before down-voting everyone? – mkoistinen Sep 22 '10 at 11:30
However, you're probably best off doing this in JavaScript
setTimeout(function() { window.location = "http://www.somedomain.com/somepage.php"; }, 5000); // 5 seconds
See @Gordon's answer a above for a more user-friendly and complete example, this is merely one method.

- 7,320
- 1
- 18
- 31
-
-1. Why a solution which will work only with JavaScript, when there is no need to? See the much more complete answer by @Gordon. – Arseni Mourzenko Sep 22 '10 at 11:17
-
3No solution will work for everyone as people can block javascript and meta refresh, I was merely giving one example. But thanks. – Alex Sep 22 '10 at 11:21
-
@MainMa I agree with Alex on this. The solution offered is fine and answers on SO dont have be to complete as the combined answers (hopefully) offer a complete picture. – Gordon Sep 22 '10 at 11:25
-
@Alex: In which case the solution by @Gordon will work (since there will be a polite message with a link to click on). Whereas yours will not. – Arseni Mourzenko Sep 22 '10 at 11:31
-
@Gordon: the problem is that less experienced developers learn by reading the answers on SO. And reading the answers like this, they will think that a pure JavaScript solution is something acceptable when making a website. That's why I downvoted it. It brings nothing additional to older answers, but shows bad practice. – Arseni Mourzenko Sep 22 '10 at 11:34
-
Except that I answered it first :-P I shall edit and point to @Gordon's solution as a better and more complete answer – Alex Sep 22 '10 at 11:51
-
@Alex it doesnt matter, but [my first revision already contained the hints to window.location and setTimeout](http://stackoverflow.com/revisions/d5cd5bb1-eb82-4e93-8b19-e29bb01b16b0/view-source). You added your code snippet while I was working on my second revision though. I upvoted you nonetheless. – Gordon Sep 22 '10 at 11:55
With META redirect you can:
<meta http-equiv="refresh" content="2;url=http://example.com/">
Where 2
is the delay in seconds.

- 14,094
- 8
- 55
- 71
Use the following code in PHP, but only after understanding this manual page fully (this is the main important part when using the following code):-
$redirectionTime = 5;
$newPageUrl = "wherever_page.php";
header( "Refresh: $redirectionTime; url=$newPageUrl" );
echo "You will now be redirected to a new page, after $redirectionTime seconds. Please be patient...";
exit();
The above code will redirect the user to the "wherever_page.php" page from the existing page after exactly 5 seconds. But you need to do another important thing.
You need to start the Output Buffer first, so that in case you output any HTML before calling the "header()
" function, no warning or fatal error will be given. In order to do this, you need to call the following function at the very first line of your web page, whether you include anything or not:-
<?php
ob_start();
// Rest of the web page logic comes after this
The main advantage of the above sets of code is that even if the JavaScript is disabled for that browser, the redirection will still occur.
Hope it helps.

- 7,955
- 13
- 49
- 92