QuickIos
answered May 5 '23 00:00
To redirect in CodeIgniter (CI), you can use the redirect() function provided by the url helper. Here are the steps to redirect to a different page:
Load the url helper in your controller. You can do this by adding the following code to your controller's constructor or method:
$this->load->helper('url');
Call the redirect() function with the URL you want to redirect to . For example, to redirect to the home page of your CI application, you can use the following code:
redirect(base_url());
The base_url() function returns the base URL of your CI application.
If you want to redirect with a specific HTTP status code, you can pass it as the second parameter of the redirect() function. For example, to redirect with a 301 (permanent) status code, you can use the following code:
redirect(base_url(), 'location', 301);
The first parameter (base_url()) is the URL to redirect to, the second parameter ('location') is the type of redirect, and the third parameter (301) is the HTTP status code.
Note that you can also use the header() function to perform a redirect in CI, but it is recommended to use the redirect() function provided by the url helper as it performs additional checks and validations.