0

Possible Duplicate:
“Warning: Headers already sent” in PHP

I have following piece of code to do the 301 redirect at the top without any spaces and nothing above this code, but its giving the error,

Warning: Cannot modify header information - headers already sent by

<?php
   require_once('../../config.php');
   header("HTTP/1.1 301 Moved Permanently");
   header("location: http://www.myapp.com/courses/mycourse-new.php");
?>

I tried using ob_start, but didn't work. I have done 301 for many files without any issues. how to resolve this issue.

Community
  • 1
  • 1
n92
  • 7,424
  • 27
  • 93
  • 129

5 Answers5

3

Make sure you have absolutely no output character (not even a blank or newline) before the <?php, both in your file and in the included file.

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
  • just adding to what Gerald Schneider said ... please note, This output character even means a space before php – Oxi Nov 30 '12 at 11:20
0

Are you using ob_start() on the first line of your code (before html tag)?

Žan Kusterle
  • 572
  • 1
  • 10
  • 28
0

You may disable output buffering with

ini_set('output_buffering', 0);

in you PHP file or set

output_buffering = Off

in php.ini and everything will work fine.

Rodion Baskakov
  • 636
  • 4
  • 14
0

Try it after removing the closing php tag ?>
Why do some scripts omit the closing php tag '?>'?
PHP closing tag

Community
  • 1
  • 1
Nandakumar V
  • 4,317
  • 4
  • 27
  • 47
-1

Use ob_start() like...

`<? 
       ob_start();
       require_once('../../config.php');
       header("HTTP/1.1 301 Moved Permanently");
       header("location: http://www.myapp.com/courses/mycourse-new.php");`

Ob_start will buffer all output untill script exit() and buffer releases. To redirect using header there should not be any other header sent already.

Alternatively you can change if you have space before

iLaYa ツ
  • 3,941
  • 3
  • 32
  • 48
Mahesh Chavda
  • 141
  • 1
  • 3