Jump to content

[general] redirect after some time


michalchojno

Recommended Posts

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?

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.