michalchojno Posted July 29, 2009 Share Posted July 29, 2009 It's more of a general question. As part of the quiz, I'd like to display some text (or picture), but only for some limited time. Then, I'd like to redirect, so another page loads. Is there a way to do that in PHP and if so how? I assume different browser/system will have different time of mainiaining the first page before redirection, correct? Quote Link to comment https://forums.phpfreaks.com/topic/167957-general-redirect-after-some-time/ Share on other sites More sharing options...
.josh Posted July 29, 2009 Share Posted July 29, 2009 you can't really do this with php. php parses the script and then sends the results to the client. There are ways to force content currently in the output buffer and you can of course have your php then "sleep" for a bit after that, but trying to redirect after that will give you a headers already sent error. The best you can do is send client-side instructions to do a timed redirect. You can do it by outputting a meta tag or a bit of js to change the location.href after x time. Quote Link to comment https://forums.phpfreaks.com/topic/167957-general-redirect-after-some-time/#findComment-885869 Share on other sites More sharing options...
michalchojno Posted July 29, 2009 Author Share Posted July 29, 2009 Telling PHP to "sleep" a little will actually do. Let's say I display a picture, then the "sleeping" will trigger, after which I will display the other contents. How can you do this "sleeping" in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/167957-general-redirect-after-some-time/#findComment-885899 Share on other sites More sharing options...
aschk Posted July 29, 2009 Share Posted July 29, 2009 You can't as crayon said, PHP is server side, it opens a connection, sends some html, closes a connection and it's done. The only way to do something after that is to have the client side (via client-side code i.e. javascript) perform a routine after a period of time. Telling PHP to sleep won't work because you've already sent your HTTP headers to the client, even if you maintain an open connection, so you can't issue an HTTP redirect. Thus, it brings me back to the fact that you have to client-side redirect. Quote Link to comment https://forums.phpfreaks.com/topic/167957-general-redirect-after-some-time/#findComment-885921 Share on other sites More sharing options...
.josh Posted July 29, 2009 Share Posted July 29, 2009 Telling PHP to "sleep" a little will actually do. Let's say I display a picture, then the "sleeping" will trigger, after which I will display the other contents. How can you do this "sleeping" in PHP? You can have it echo the picture, force output to the browser, sleep, and force more output on the same page, but you cannot make it redirect after output, because headers have already been sent. If you are willing to have it on the same page instead of redirecting, you would use echo to echo out the picture, ob_flush to force it to output what's currently in the output buffer, sleep to time out for a bit, and then echo to display the rest of the content. Quote Link to comment https://forums.phpfreaks.com/topic/167957-general-redirect-after-some-time/#findComment-885941 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.