CodeIgniter 3 redirect function not working CodeIgniter 3 redirect function not working codeigniter codeigniter

CodeIgniter 3 redirect function not working


Add ob_start(); to Controller

<?phpob_start();defined('BASEPATH') OR exit('No direct script access allowed');class Cart extends CI_Controller { // code here}


Through a lot of trial and error, I've since discovered my problem. When I performed var_dump(base_url) I found out that I was $_SERVER['SERVER_ADDR'], and since I was working locally, that is ::1. What I learned was base_url wasnt working like it was in CI2. Sadpanda.

Base_url config variable was incorrectly set. In config/config.php my base_url was set using a single quote. Allowing CI3 to guess the protocol, it would default to $_SERVER['SERVER_ADDR'], which would return ::1 since I am working locally. Furthermore, when I set the variable to 'http://localhost/supersecretname', using single quotes the exact same problem popped up as when it was using the auto SERVER_ADDR

By some miracle of sleep deprivation, I was trying many different values here, and at some point I switched from single, to double quotes.

And thus, a solution was found.

$config['base_url'] = "http://localhost/supersecretname";


Add base_url() in redirect function.

 redirect(base_url().'user', 'refresh');