CodeIgniter redirect: white page

The problem with CodeIgniter redirect

I’ve wasted two hours with this issue (CodeIgniter redirect leading to a white page) so I thought I’d write a post about in case someone has the same problem. I’m currently writing a software using CodeIgniter. When you login, if the login validation succeeds, you are redirected to the home page, otherwise an error is shown and you stay on the login page. Here’s how my login controller looks like:

class Login extends CI_Controller {

	function index() {
		$this->load->view('login', $this->data);
	}

	function process() {
		$this->load->library('form_validation');
		
		[...]
		
		if ($this->form_validation->run() == FALSE) {
			$this->load->view('login', $this->data);
		}
		else {
			// Go to private area
			redirect('home', 'refresh');
		}
	}
}

I am usually coding on a Mac using either MAMP or native PHP/MySQL and sometimes on a Windows machine. With all 3 combinations, it did work fine. But the server on which the software will be hosted is a Debian machine. So I after I was done with some parts of the software I uploaded it to the server. The login screen was displayed just fine but after logging in, I just go a white page a.k.a PHP white screen of death.

The first thing I did was to check my PHP error log file. Nothing there. Then I checked the CodeIgniter application log file. Nothing there either. The source code of the page in the browser was completely empty. So I thought that I just didn’t see an error message because the log levels weren’t set properly. First I changed the log level in php.ini:

error_reporting = E_ALL | E_STRICT

And restarted the apache web server. This didn’t help. Still no log file entry related to my problem.

Then I increased the log level in CodeIgniter (in /application/config/config.php):

$config['log_threshold'] = 4;

This didn’t help either. I could see that everything was fine in the log file.

I also tried forcing the display of error message by adding this in my controller (and finally also in index.php) but it didn’t work either:

error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);

Without a clue, I added error_log commands before every line in my controller. I could then see in the PHP error line that everything up to the redirect was working fine. So the redirect function was my problem.

The first check was to see whether the url helper was properly loaded. In /application/config/autoload.php:

$autoload['helper'] = array('url', 'form');

So it’s fine. Actually you can see it in the CodeIgniter application log file:

DEBUG - 2013-08-03 22:52:47 --> Loader Class Initialized
DEBUG - 2013-08-03 22:52:47 --> Helper loaded: url_helper
DEBUG - 2013-08-03 22:52:47 --> Helper loaded: form_helper
DEBUG - 2013-08-03 22:52:47 --> Database Driver Class Initialized
DEBUG - 2013-08-03 22:52:47 --> Session Class Initialized
DEBUG - 2013-08-03 22:52:47 --> Helper loaded: string_helper
DEBUG - 2013-08-03 22:52:47 --> Session routines successfully run
DEBUG - 2013-08-03 22:52:47 --> Controller Class Initialized

So the CodeIgniter redirect function was available (anyway I didn’t get a message it was missing, so it couldn’t have been the problem). So I checked the documentation regarding the CodeIgniter redirect function:

Note: In order for this function to work it must be used before anything is outputted to the browser since it utilizes server headers.

So I checked the code for some echo commands which might be executed before the CodeIgniter redirect. I also looked for spaces or line breaks before my opening <?php tags. But found nothing. Anyway, if this was the problem I should see somewhere something like this:

Cannot modify header information - headers already sent

The solution

Then I noticed the following in the CodeIgniter redirect documentation:

The optional second parameter allows you to choose between the “location” method (default) or the “refresh” method. Location is faster, but on Windows servers it can sometimes be a problem.

I actually didn’t really know why I was using refresh. I had just reused some code found somewhere. Since I do not plan to run my software on Windows servers, I though I’d just try removing the refresh so that I use location and see whether the problems still occurs:

redirect('home');

And there was my page ! The difference between location and refresh in CodeIgniter redirect is just that refresh does the following:

header("Refresh:0;url=".$uri);

and location:

header("Location: ".$uri, TRUE, $http_response_code);

Where the default value of $http_response_code is 302.

So I still do not know what the actual problem with refresh was and why it only occurred on our Linux server and on none of the Mac and Windows machines I use. But the┬áCodeIgniter redirect issue is solved now and I’ve already moved on to the next issue.