CodeIgniter Forums
Redirect not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Redirect not working (/showthread.php?tid=33997)



Redirect not working - El Forum - 09-15-2010

[eluser]Larry Wakeman[/eluser]
I am doing a logout by passing an additional segment 'logout' so I am going to /foo/bar/logout. In the logout code in the index function of my controller (I am using a single controller with a wild card route and letting the controller decide if the page exists and to display the correct content) so I put the following in:

Code:
if (stripos(uri_string(), 'logout') !== false)
{
    // perform a logout
    $this->cicms_login->logout();
    unset ($this->a_uri[(count($this->a_uri))]);
//    redirect(substr(uri_string(), 0, stripos(uri_string(), 'logout')-1));
    redirect('/foo/bar', 'refresh');
}
// display the page
As it is, the page is redirected to /foo/bar/foo/bar.


Redirect not working - El Forum - 09-15-2010

[eluser]Larry Wakeman[/eluser]
I found the problem. Seems that it works with a full url as follows:

Code:
if (stripos(uri_string(), 'logout') !== false)
        {
            // perform a logout
            $this->cicms_login->logout();
            unset ($this->a_uri[(count($this->a_uri))]);
            redirect('http://'.$_SERVER['SERVER_NAME'].substr(uri_string(), 0, stripos(uri_string(), 'logout')-1));
        }
        // display the page



Redirect not working - El Forum - 09-15-2010

[eluser]danmontgomery[/eluser]
Sounds like you have a bad value set in $config['base_url'].


Redirect not working - El Forum - 09-16-2010

[eluser]Larry Wakeman[/eluser]
Yeah, that was the problem. Now if I change the config.php line:

Code:
$config['base_url']    = "http://example.com/";

to:

Code:
$config['base_protocol'] = 'http';

if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' && $config['base_protocol'] == 'http')
{
    $config['base_protocol'] .= 's';
}

$script_path = preg_replace('/(.+)index\.php$/', '$1', $_SERVER['SCRIPT_NAME']);
$config['base_url']    = $config['base_protocol']."://".$_SERVER["HTTP_HOST"].$script_path;

I will never have that problem again.