user188962
user188962

Reputation:

PHP redirect AFTER sending headers?

I have a function which needs to redirect the page if a variable is set etc...

The problem is, this function is at the bottom of the php page.

This means, I have outputted alot of information, so I get a headers warning.

"Warning - Headers already sent by ..."

Is there any way to redirect after headers are sent?

Thanks

Upvotes: 7

Views: 11594

Answers (2)

Piskvor left the building
Piskvor left the building

Reputation: 92762

There are ways, but they're basically workarounds:

The simplest one is the meta http-equiv:

<meta http-equiv="Refresh" content="0;http://www.example.com/newlocation">
  • some browsers will not like this when it's outside of the <head> element, and drop into quirks mode

Or, you can try the JavaScript redirect:

<script>
    window.location = 'http://www.example.com/newlocation';
</script>
  • which obviously won't work without JavaScript.

Upvotes: 13

damusnet
damusnet

Reputation: 4398

What you should do is put ob_start() at the very beginning of your page, and ob_flush() at the very end. This way you don't run into headers already sent errors.

See those functions for further reference.

Upvotes: 24

Related Questions