Headers already sent php - dealing with legacy code

119 Views Asked by At

I have seen this question posted a thousand times, but what I haven't seen is an answer to the following situation. Maybe one already exists?

Somethings I have made sure of:

  • There are NO spaces around <?php and ?>.
  • There are no echo statements.
  • There are no print statements or any other "output data statements"

There is one issue:

The code in question is being "included on a "layout page" which is causing the issue, maybe some one could tell me how to get around that?

So heres the code, I did not write this, I am just maintaining it.

$pagetitle = $_SERVER['REQUEST_URI'];
switch ($pagetitle) {
  ...
  case "/locations.php?l=8" :
    echo '<title>Mississauga West, Canada Winemaking - Vinbon</title>';
  break;
  ...
}

With in that case statement I have tried to do the following, knowing it probably wouldn't work because of the echo statement.

case "/locations.php?l=8" :
  ob_start();
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: http://www.New-Website.com");
  die();
  ob_end_flush();
break;

Now I know you guys have stated in some answers not to set an echo above as this could cause issues. I assume ob_start() would clear that up?

As you can See this must be a 301 redirect.

Now this piece of code lies in a file called: inc_meta.php which is then included (not required) in a file called layer01.php.

It causes, when run, the following "Warnings":

Warning: Cannot modify header information - headers already sent by (output started at /home/content/46/11552446/html/layout/layer01.php:5) in /home/content/46/11552446/html/layout/layer01.php on line 10

Warning: Cannot modify header information - headers already sent by (output started at /home/content/46/11552446/html/layout/layer01.php:5) in /home/content/46/11552446/html/layout/layer01.php on line 11

Now Line 5 is where we do <? include inc_meta.php ?> and the layer01.php is nothing more then an html file.

Line 10 is a space above the <body> tag and line 11 is the <body> tag.

I cannot use javascript because this must be a 301 redirect for this location. Does any one have any ideas?

Update 1

So I tried removing the echo statement as I figured you guys would state that was the issue

I now get:

Warning: Cannot modify header information - headers already sent by (output started at /home/content/46/11552446/html/layout/layer01.php:5) in /home/content/46/11552446/html/layout/inc_meta.php on line 51

Warning: Cannot modify header information - headers already sent by (output started at /home/content/46/11552446/html/layout/layer01.php:5) in /home/content/46/11552446/html/layout/inc_meta.php on line 52

lines 51 and 52 refer to:

header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.New-Website.com");
1

There are 1 best solutions below

0
On

The issue was that in the php/html file I did not know that you had to include this file above the <DOCTYPE ... > and <html> tags. Once it was included there the issue fixed its self.

So in my case:

<? include('inc_meta.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
....

Instead of what it was:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<? include('inc_meta.php'); ?>
...