1

how to jump to other page after header had been sent ?

I have some codes writed in my template file, I want to using php to jump to other page when some condition is true, but I can not use header command here ,for the header info had been sent in other php file before this template file. then how can I jump to other url now ? only the way to use js?

Warning: Cannot modify header information - headers already sent by ...
mingfish_004
  • 1,385
  • 2
  • 18
  • 26

3 Answers3

2

Use output buffering and header clearing to achieve this.

ob_start()
...
if (!headers_sent()) {
  foreach (headers_list() as $header)
    header_remove($header);
}
ob_flush_end()

Untested, but give it a shot.

CP510
  • 2,297
  • 15
  • 15
1

you have two options.

  1. Move your header location before sending any output.
  2. use a client side redirection and you have two ways to do this

a/ using javascript

window.location = "http://www.yoururl.com";

b/ using meta

<META http-equiv="refresh" content="5;URL=http://www.yourlink.com/new-link">

The best solution here How to fix "Headers already sent" error in PHP

Community
  • 1
  • 1
mbouzahir
  • 1,424
  • 13
  • 16
1

The anaswer from CP510 is good. Another way would be to use javascript to redirect the page. To quote Ryan McGeary answer, you could do either

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

or

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
Community
  • 1
  • 1
Michael Villeneuve
  • 3,933
  • 4
  • 26
  • 40