Jump to content

How to do redirecting after 5 seconds


yoursurrogategod

Recommended Posts

Hi, I have an idea for a feature for an application. I'd like to display an error/warning/note message for the user and then redirect them back to a previous page after 5 (or more) seconds. The thing is, I don't know how... how can I do this with PHP? Can I do this with PHP? Would I need to get Javascript involved?

Link to comment
https://forums.phpfreaks.com/topic/273925-how-to-do-redirecting-after-5-seconds/
Share on other sites

Hi , is this good for you?

 

 

<?php

echo '<html><head><body><meta http-equiv="refresh" content="5; url=PUT_HERE_THE:PAGE_LINK"></head></body></html>';

?>

 

Just remove PUT_HERE_THE:PAGE_LINK and put the page link (http://blabla.com/page.html) or the page name (page.html)

 

or use the php redirect and a sleep

 

 

<?php

sleep(10);

header("Location: http://blabla.com/");

?>

 

i prefer the first method.

I would definitely go with the Meta tag approach rather than Javascript. It is a feature implemented in the core HTML and wouldn't have any problems due to Javascript being enabled or not.

 

However, this

<?php

sleep(10);

header("Location: http://blabla.com/");

?>

 

Would definitely not work. A page is not delivered to the browser until processing is complete. Therefore, that sleep() command would simply cause the user to view a blank page for 10 second before ultimately being redirected to the second page - i.e. they would never see the first page with the message.

I would definitely go with the Meta tag approach rather than Javascript. It is a feature implemented in the core HTML and wouldn't have any problems due to Javascript being enabled or not.

 

However, this

 

 

Would definitely not work. A page is not delivered to the browser until processing is complete. Therefore, that sleep() command would simply cause the user to view a blank page for 10 second before ultimately being redirected to the second page - i.e. they would never see the first page with the message.

 

Yeah you are right and i prefer the meta tag too

 

but what if

<?php

echo You are watching the error;

sleep(5);

header("Location: http://blabla.com/");

?>

Yeah you are right and i prefer the meta tag too

 

but what if

<?php

echo You are watching the error;

sleep(5);

header("Location: http://blabla.com/");

?>

 

But, what if what? That would never work for two reasons.

 

First, as I stated before, when the browser requests a PHP page from the server. The server will retrieve the document and process it (and any included files) in its entirety. Once all the processing is complete the server will THEN send the completed page to the browser. The server does not send bits and pieces of the page to the browser as it encounters each echo.

 

Second, a header() call cannot be made AFTER content is sent for output. Therefore, the echo statement above would cause the header() to produce the error:

Warning: Cannot modify header information - headers already sent . . .

 

So, the end result of that script is that the user would be looking at a blank page for 5 seconds (because the script has not completed processing), then the user will see the error message. The user would never see the echo statement at all and wouldn't even get redirected. So, that script not only fails - it fails twice.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.