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.

8 thoughts on “CodeIgniter redirect: white page

  1. This type of error occurs because header is already sent to server and when u again request for this type of thing header is not loaded only content goes hence it says header already sent.

    Use ob_start();

    after your first php tag in error page and in place of redirect

    $urls=base_url();
    $this->load->helper(‘url’);

    and then
    redirect($urls, ‘refresh’);

    1. Here it is definitely not the case, since both commands do sent a header and as I mentioned above I would then get an error message saying “Cannot modify header information – headers already sent” which was not the case. Of course if you get “Cannot modify header information – headers already sent” then you need to make sure that you do not already send the header before calling refresh.

  2. Thank you
    I found something finall working for me here !
    Just changes url_helper

    if ( ! function_exists(‘redirect’))
    {
    function redirect($uri = ”, $method = ‘location’, $http_response_code = 302)
    {
    if ( ! preg_match(‘#^https?://#i’, $uri))
    {
    $uri = site_url($uri);
    }
    header(“Location: “.$uri, TRUE, $http_response_code);//vivek modified
    /*switch($method)
    {
    case ‘refresh’ : header(“Refresh:0;url=”.$uri);
    break;
    default : header(“Location: “.$uri, TRUE, $http_response_code);
    break;
    }*/
    exit;
    }
    }

  3. Man thanks for these information it helped me to find the problem on my CI app. The problem with mine it was a core library that I extended and in some part of a if statement on construct of this library I had put “exit();”. This class was in autoload and because of that the system was terminating unexpectedly with a blank screen, with your error checklist I could find the problem.

  4. I had the same problem, I had never had anything like this in all the time I used Codeigniter, I tried your solution and it worked perfectly!

    Thank you!
    regards

Leave a Reply

Your email address will not be published. Required fields are marked *