By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

header issue

zorgi
Expert 100+
P: 431
Hi guys

Its me again :)
I have a small problem with redirecting using headers. When I do for example this

[PHP]
header('Location: index.php');
[/PHP]

Everything works fine. User gets redirected and looks at the page I wanted them except that if I had any code after that statement its still gonna be executed. I came up with somewhat sloppy solution of if else blocks and ... well as I said it doesn't look nice. Is there a better way.

Also one more question. Sometime when header like above doesn't work because of necessary outputs to the page I simply do this:

[PHP]
echo "<script language='javascript'>\n";
echo "window.open('".$redirect_url."', '_parent')";
echo "</script>\n";
[/PHP]

It works but also looks bit sloppy to me. Is there better solution to this problem. Thank you in advance for your input.
Oct 15 '08 #1
Share this Question
Share on Google+
4 Replies


Dormilich
Expert Mod 5K+
P: 8,639
[PHP]header('Location: index.php');
exit;
# further code [/PHP]
Oct 15 '08 #2

zorgi
Expert 100+
P: 431
[PHP]header('Location: index.php');
exit;
# further code [/PHP]
Lovely. Thank you :)
Oct 15 '08 #3

Atli
Expert 5K+
P: 5,058
As to your other question.

You can use Output Buffering to control exactly when the output is sent.

For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo "Something very important";
  3. header("Location: index.php");
  4. ?>
  5.  
This would cause and error and remain on the same page.
But this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ob_start();
  3. echo "Something very important";
  4. header("Location: index.php");
  5. ob_end_flush();
  6. ?>
  7.  
would redirect you to "index.php".

Technically the output would still be sent, but you would be redirected before you ever saw it.
Oct 15 '08 #4

zorgi
Expert 100+
P: 431
Thank you so much guys. I knew there must be better solution to this and now I know it.
Oct 15 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.