Redirect with CodeIgniter

In CodeIgniter, you can use the redirect() function to redirect the user's browser to a new page. This function is part of the CodeIgniter's URL Helper, so you will need to load the URL Helper before using it.

Here's an example of how to use the redirect() function:

// Load the URL Helper
$this->load->helper('url');

// Redirect the user's browser to the desired URL
redirect('http://example.com/mypage');

Watch a course Learn object oriented PHP

You can also use the redirect() function to redirect the user to a different controller or method within your CodeIgniter application. For example:

// Redirect the user to the "welcome" controller
redirect('welcome');

// Redirect the user to the "welcome" controller's "greeting" method
redirect('welcome/greeting');

The redirect() function also allows you to pass additional segments to the destination URL. For example:

// Redirect the user to the "welcome" controller's "greeting" method,
// passing the user's name as a URL segment
redirect('welcome/greeting/John');

You can also use the redirect() function to pass flash data, which will be available only for the next server request. This can be useful for displaying messages to the user after a form submission or other action.

// Set a message to be displayed to the user
$this->session->set_flashdata('message', 'Your form was successfully submitted!');

// Redirect the user to the "welcome" controller
redirect('welcome');

Then, in your "welcome" controller or view, you can retrieve and display the flash data message like this:

echo $this->session->flashdata('message');