1

when I redirect my page to another page header("location:popup.php"); it gives following error -

Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\303\levelupdate.php:289) in C:\xampp\htdocs\303\levelupdate.php on line 320

scunliffe
  • 62,582
  • 25
  • 126
  • 161
pamela
  • 13
  • 2
  • 3
    This is a common problem and was already asked very often. Search for it here on SO: http://stackoverflow.com/search?q=php+headers+already+sent , you should find something that helps you. – Felix Kling Jun 06 '10 at 16:01

5 Answers5

4

You can only do redirect if nothing has been sent to the browser already. This means you have to do one of two things:

  1. Work out if you need to redirect before you send any data.

  2. Use ob_start to hold the page until it has finished processing. As soon as you throw a header call, it will dump the buffered copy and just send the header. But you have to call ob_start right at the beginning so it catches any output. Its PHP page should give you some clues on this.

My personal preference lies with output buffer (ob) but it will mean no data gets sent to the client until the whole page is done rendering. This can sometimes make the site seem a little slow if the page takes a long time to generate.

You can combat slowness by calling ob_flush which sends the contents of the buffer to the client. I recommend doing this as soon as you know you won't need to send a header change. But it's completely optional and not worth the effort for simple and quick scripts.

Oli
  • 235,628
  • 64
  • 220
  • 299
0

This error means some body text has been sent to the browser. This isn't allowed, because headers always go first. Maybe there is whitespace, or have you echo'd HTML?

The output might also be a previous error. header('location: ...') requires a fully-qualified URL (i.e. with http://domain.com).

Amy B
  • 17,874
  • 12
  • 64
  • 83
0

the header command only works if no output has been sent to the page. Therefore you need to ensure that no html or any other output like php echos has been sent to the page before being called.

Check if you have empty rows on the top of your file before your php declaration.

Luis
  • 5,979
  • 2
  • 31
  • 51
0

it means that you have already output some content (i.e. echo) before doing header("location:popup.php") and therefore it won't work.

S P
  • 4,615
  • 2
  • 18
  • 31
0

You need to ensure that nothing is written after a header declaration.

Detailed explanation

To fix, put exit; under your code.

Kieran Andrews
  • 5,845
  • 2
  • 33
  • 57
  • I mean that you dont have any code after a header ( "Location: " ); /* Redirect browser */ /* Make sure that code below does not get executed when we redirect. */ exit; ?> so that it isnt executed when the new page is loaded. – Kieran Andrews Jun 06 '10 at 16:25
  • But this would not fix the problem here. I agree that exiting after a location change is a good idea but it's certainly not illegal to send data along with a header (it's just ignored) and it certainly wouldn't throw an error if you did. The issue is that there is data sent to the client (after a "200 OK" header). What the location header does it change that to a 302 header but it can't be changed if a 200 has already been sent. – Oli Jun 06 '10 at 16:33