Home » Php » javascript – PHP redirect AFTER sending headers?

javascript – PHP redirect AFTER sending headers?

Posted by: admin July 12, 2020 Leave a comment

Questions:

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

How to&Answers:

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.

Answer:

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.