Recently I moved my WIP site from a Windows XP machine with WAMP to a MacBook with MAMP. As I test the site I'm finding that the redirects (i.e., $this->redirect="Index.php";
) don't seem to be working. They worked fine on the Windows XP machine.
I use a front controller pattern. The index page looks like that:
Index.php:
<?php
.....
switch ($action){
case 'logoff':
require_once('Controller_Logoff.php');
$command=new controller_Logoff();
break;
case 'register':
require_once('Controller_Register.php');
$command=new controller_Register();
break;
...
}
...
$command->execute($view);
$menu=array(
new MenuEntry("Logoff","Index.php",array("action"=>"logoff")),
new MenuEntry("Register","Index.php", array("action"=>"register")),
....
) // This menu is shown on the user's view
if ($command->getRedirect()){ //This case doesn't work
header('Location:'.$command->getRedirect());
}else if ($command->getInclusion()){ //This case works
include ("UI_Header.php");
include ("UI_Menu.php");
include ("UI_Message.php");
echo "<div class='content'>";
include ($command->getInclusion());
echo "</div>";
include ("UI_Footer.php");
}
The problem seems to occur at this stage: header('Location:'.$command->getRedirect());
Note that it still fails if I do header('Location: http://localhost:8888/'.$command->getRedirect());
.
I also tried to include ob_start();
in my script, to no effect.
An example on one of the controller pages (at the end of a new user registration) I have:
Controller_register.php:
<?php
class Controller_Register extends Controller {
protected $inclusion='UI_Register.php';
function execute($view){
.... // Code to register a new user - that works, i.e., a new user appears in the DB
var_dump("Before redirect");
$this->redirect="Index.php";
var_dump("after redirect");
}
The redirect
is a variable in the class used for the various controllers. It's set as null
in the parent class. I have verified that it is changed to its correct value (Index.php
) by the time the header should be executed.
When I run this code, I get a blank page. The exact course of events is:
- Open the site - URL is
http://localhost:8888/Project/
- Click on the menu item
register
which sends me to the registration page, which URL ishttp://localhost:8888/Project/Index.php?action=register
- Enter the required info (username, PW, and email) in the registration form and click
submit
- the information is successfully loaded into the db. - Shown a blank page - the URL doesn't change and remains
http://localhost:8888/Project/Index.php?action=register
but obviously the file (the registration page) isn't loaded.
The error log from the time I start clicking on the register
link yields the following:
- [09-Oct-2014 23:10:00 Europe/Berlin] PHP Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/Project/Index.php:1) in /Applications/MAMP/htdocs/Project/UI_Header.php on line 2
- [09-Oct-2014 23:10:07 Europe/Berlin] PHP Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/Project/Index.php:1) in /Applications/MAMP/htdocs/Project/Index.php on line 13