-1

I have a header.php file, which is included on every page of a site and it contains things like the information and the opening tag.

Now whenever I try to send header information from another page on the site I know I can't do that because I have already sent HTML to the client computer. So I created a function after much googling and stuck it at the top of the Header.php file:

   ob_start();
function redirect($url){
    flush();
    ob_flush();
    header("Location: $url");
    die;
}

And I can call that from anywhere else in the site but I still get an error. I'm probably doing this function all wrong, but that's what google will do sometimes. Any help to fix this would be great, thanks.

EDIT: The error I get is "Cannot Modify Header Information - Header already sent by ...". I already have this problem fixed.

Joe J
  • 37
  • 1
  • 9
  • 1
    `ob_end_clean()` might be more sensible than flushing. But we don't really know the remaining code or usage, so .. – mario Jun 25 '12 at 22:37
  • do you by any chance have any white space at all at the start of the php file? Any white space can in my experience anyway can cause this problem. – Jon Taylor Jun 25 '12 at 22:38

4 Answers4

1

flush() writes the buffer to the browser - the opposite of what you want. Remove the flush lines. Look at http://us2.php.net/manual/en/function.ob-clean.php if you want to clear the buffer.

Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
0

You should say what "error" you are getting, but if you are including this function anywhere on the page after anything is outputted to the browser, you will get a "cannot modify header information" error.

It doesn't matter if you put it in a function or not, it has to come before anything is outputted to the browser.

Also, why do you have flush() included with this function? That may keep the redirect from working as well.

MultiDev
  • 10,389
  • 24
  • 81
  • 148
0

I believe

ob_end_clean();

is the function you are looking for. So instead of both the flush() and ob_flush() functions.

Luc
  • 5,339
  • 2
  • 48
  • 48
0

You can just add @ for header function

@header("Location: $url");

But it`s not good solution.

When you are sending data to browser, you also send header with 200 status, then you are trying to send 302 status (for redirecting), so, the problem becomes from sending 200 status.

You can read more about headers at http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

Best regards, Zhirayr G.

Mark Pegasov
  • 5,109
  • 9
  • 26
  • 30