标题发送后如何跳转到其他页面? [重复]

This question already has an answer here:

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 ...
</div>

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.

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

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";