0

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");
SeekingTruth
  • 1,044
  • 2
  • 15
  • 23
  • first mistake i see .. you use echo before ob_start() that produces output and header didn't work anymore – donald123 Dec 12 '14 at 17:49
  • I figured as much I did remove it, same error. @donald123 – SeekingTruth Dec 12 '14 at 17:50
  • @donald123 Please see the updated version. – SeekingTruth Dec 12 '14 at 17:53
  • What I suggest you do is remove the includes/requires one by one till you don't get anymore errors. Then, add them back in one by one, till you do start getting errors. Only then, will you know which one is causing havoc. Plus, don't leave a byte order mark out of the equation neither. That also counts as output. – Funk Forty Niner Dec 12 '14 at 17:53
  • There is only one @Fred-ii- and the only one on this page is `inc_meta.php` The errors are now coming from this particular php. – SeekingTruth Dec 12 '14 at 17:54
  • 3
    What happens above line 5? in layer01.php? My understanding is that once you begin sending information, you can't adjust the headers. So if you're embedding that php code in an html file, like `` then you won't be able to modify headers because `` has already been sent. – Chuck Dec 12 '14 at 17:55
  • Chuck is correct. You will need to modify layer01.php to actually _be_ PHP from the top of the file until the include file. If you have HTML above the include file, you will need to start output buffering at the top of layer01.php. – Kevin_Kinsey Dec 12 '14 at 17:57

1 Answers1

1

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'); ?>
...
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
SeekingTruth
  • 1,044
  • 2
  • 15
  • 23