0

Possible Duplicate:
Headers already sent by PHP

Environment

  • Apache 2.2.22
  • PHP 5.4.4
  • MAC OSX Lion

Disclaimer

I'm very new to PHP and the entire community, and so I'm still learning my way around.

Objective

Redirect to a very specific URL.

Code

I have built my own MVC handler, which I've included below, and I'm trying to handle a specific situation where I am currently at user/login but I want to redirect back to the home page after logging in so I want to go to home/index. To do this, I thought, the right way to redirect (after a good bit of searching) was to issue the following command:

header("Location: http://$host$uri/$controllerName/$action");

I have verified that the URL is correct (e.g. if I put the URL in the address bar it would work), but for some reason the browser isn't responding.

Can somebody point me in the right direction?

UPDATE

Below is the error I'm receiving.

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/test/core/bootstrap.php:66) in /Applications/MAMP/htdocs/test/core/bootstrap.php on line 70

And here is line 66:

if ($_SERVER['DEBUG']) { echo "<br/>$url ..."; }

So, I just can't output anything before trying that it appears?

Community
  • 1
  • 1
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232

1 Answers1

2

You can only modify header information BEFORE any output has started. Try putting your header change at the top of your script and it will work (assuming those variables are set).

At line 66, you probably have an echo command or some HTML.

From the PHP Manual:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

kmoney12
  • 4,413
  • 5
  • 37
  • 59