Redirect with CodeIgniter
Solution 1:
redirect()
URL Helper
The redirect statement in code igniter sends the user to the specified web page using a redirect header statement.
This statement resides in the URL helper which is loaded in the following way:
$this->load->helper('url');
The redirect function loads a local URI specified in the first parameter of the function call and built using the options specified in your config file.
The second parameter allows the developer to use different HTTP commands to perform the redirect "location" or "refresh".
According to the Code Igniter documentation: "Location is faster, but on Windows servers it can sometimes be a problem."
Example:
if ($user_logged_in === FALSE)
{
redirect('/account/login', 'refresh');
}
Solution 2:
If your directory structure is like this,
site
application
controller
folder_1
first_controller.php
second_controller.php
folder_2
first_controller.php
second_controller.php
And when you are going to redirect it in same controller in which you are working then just write the following code.
$this->load->helper('url');
if ($some_value === FALSE/TRUE) //You may give 0/1 as well,its up to your logic
{
redirect('same_controller/method', 'refresh');
}
And if you want to redirect to another control then use the following code.
$this->load->helper('url');
if ($some_value === FALSE/TRUE) //You may give 0/1 as well,its up to your logic
{
redirect('folder_name/any_controller_name/method', 'refresh');
}
Solution 3:
first, you need to load URL helper like this type or you can upload within autoload.php file:
$this->load->helper('url');
if (!$user_logged_in)
{
redirect('/account/login', 'refresh');
}
Solution 4:
If you want to redirect previous location or last request then you have to include user_agent
library:
$this->load->library('user_agent');
and then use at last in a function that you are using:
redirect($this->agent->referrer());
its working for me.