6

I am trying to redirect a page after successful execution. However, I want to display a message to the user (e.g. 'Changed made. Redirecting...') while the redirection is done. Changing header variables after output in the page causes errors in PHP and I know this. My question is, how should I do this?

My current code is something like

// ... execution code
echo 'Changes made successfully. Now redirecting...';
header('Location: index.php');

and this doesn't work. I have also seen an answer on SO suggesting I use ob_start() and ob_flush() at the start and end of my page, respectively. But that didn't solve my problem either, I still get the error.

NB I need to use PHP only, I don't want JavaScript for redirection.

Edit: I ended up using JavaScript for redirection, as I needed to output some useful message to the user before redirecting, and PHP alone isn't the best solution for that.

mavili
  • 3,385
  • 4
  • 30
  • 46
  • I think you can find the answer [here](https://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php?rq=1). – MJay Jun 07 '17 at 04:46

4 Answers4

2

first, you cant use "header" after echo or any output. second, why you need php for it? you can use javascript or best, just put it in the html like it says here

Dima
  • 8,586
  • 4
  • 28
  • 57
  • that's not correct. after echo'ing anything out, PHP gives error and says you can't change header information as output has already been sent. I ended up using javascript – mavili Apr 23 '13 at 19:03
  • what is not correct? thats what i sayd, you cant use header after echo or output – Dima Apr 23 '13 at 19:20
  • oh, Sorry!! I read that as 'can' and not 'cant'. My mistake. well that happens when you have spelling mistakes :P It should be can't :) – mavili Apr 23 '13 at 22:47
2

Echo after setting the header.

header("refresh:1;url=test.php"); 
echo 'this is a test';
exit;
TheRealJAG
  • 1,978
  • 20
  • 16
  • This works fine except missing the closing " after test.php. Cannot edit because "edit must contain at least 6 characters"... – Woody Sep 04 '18 at 22:50
0
header( "refresh:5;url=wherever.php" );

You can redirect the page after 5 seconds to wherever.php. Please see this question: Page redirect after certain time PHP

Community
  • 1
  • 1
Barış Akkurt
  • 2,255
  • 3
  • 22
  • 37
  • I like this method. However, you should have mentioned that this line will have to go BEFORE any output. as it will wait 5 seconds before redirection, the message can be output after this line and still displayed for that duration. – mavili Apr 21 '13 at 13:44
  • 1
    If you want to send a message, you can also send this refresh header as a meta tag like it was invented. The "Refresh" header is not in any standard, it just happens to work with some clients. Also note that using this (either META tag or HTTP header) breaks the back button in the browser. See all the remarks from W3C listed here: http://en.wikipedia.org/wiki/URL_redirection#Refresh_Meta_tag_and_HTTP_refresh_header – Sven Apr 21 '13 at 14:34
0

Drawback of redirection by all three popular methods

 header('Location:$url'); 
 echo "<meta http-equiv='refresh' content='3;$url>";
 echo "<body onload='window.location=$url'>";

is that you need to decide about possible redirection before any part of the page is displayed to user. When the decision process is time consuming, user will gawp at blank page. I use deferred redirection with Javascript OnLoad event, which is declared at the top of page but it does the actual redirection only if a hidden object with id='Redirect' is echoed anywhere later in the HTML body.

echo "<html><body onload='if (typeof(Redirect)!=undefined) 
setTimeout(function()
    {window.location=document.getElementById(\"Redirect\").value},3000);'>";
echo $HtmlBody; echo $FormToFill;flush();
if (CheckReceivedDataFailed()) echo "Wrong data, please fill the form again";
else echo "<input type='hidden' id='Redirect' value='$url'>
           Thanks, youll be redirected in 3 sec";
echo "</body></html>";
vitsoft
  • 5,515
  • 1
  • 18
  • 31
  • better echo a link that people can get redirected (or rather redirect themselves) even if the js breaks (for example not loaded properly) (happens frequently on mobile internet) but I like the Idea. I'll try it. – My1 Dec 16 '15 at 09:00