yoursurrogategod Posted February 1, 2013 Share Posted February 1, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/273925-how-to-do-redirecting-after-5-seconds/ Share on other sites More sharing options...
Jessica Posted February 1, 2013 Share Posted February 1, 2013 You need to use Javascript. Quote Link to comment https://forums.phpfreaks.com/topic/273925-how-to-do-redirecting-after-5-seconds/#findComment-1409574 Share on other sites More sharing options...
TuQuoQueBrute Posted February 1, 2013 Share Posted February 1, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273925-how-to-do-redirecting-after-5-seconds/#findComment-1409584 Share on other sites More sharing options...
Psycho Posted February 1, 2013 Share Posted February 1, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273925-how-to-do-redirecting-after-5-seconds/#findComment-1409586 Share on other sites More sharing options...
TuQuoQueBrute Posted February 1, 2013 Share Posted February 1, 2013 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/"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/273925-how-to-do-redirecting-after-5-seconds/#findComment-1409595 Share on other sites More sharing options...
Psycho Posted February 1, 2013 Share Posted February 1, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273925-how-to-do-redirecting-after-5-seconds/#findComment-1409603 Share on other sites More sharing options...
TuQuoQueBrute Posted February 1, 2013 Share Posted February 1, 2013 got it Quote Link to comment https://forums.phpfreaks.com/topic/273925-how-to-do-redirecting-after-5-seconds/#findComment-1409606 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.