Jump to content

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.

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.