CodeIgniter Page Redirection
While building web application, we often need to redirect the user from one page to another page. CodeIgniter makes this job easy for us. for which there are many functions which are as following
redirect()
The redirects functions accepts two parameters to execute the function first is 'Location Url' and second parameter allows the developer to use different HTTP commands to perform the redirect "location" or "refresh".
Syntax:
redirect($uri = '', $method = 'auto', $code = NULL);
Parameter Description:
- $uri (string) – this parameter represents the URI string. It has two possible values. It can be either a full site URL or a URI segment. It is mandatory.
- $method (string) – This parameter represents the Redirect method. It has three possible values: 1. auto 2. location 3. refresh. By default, auto is selected. It is mandatory.
- $code (string) – This parameter represents HTTP Response code (usually 302 or 303). It is an optional parameter.
- The return type of this function is void.
Example
Open the application/controllers directory and create a new controller Redirect Controller.php
<?php
class Welcome extends CI_Controller {
public function index() {
/*Load the URL helper*/
$this->load->helper('url');
/*Redirect the user to Stack Overlode site*/
redirect('https://www.stackoverlode.com');
}
public function google() {
/*Load the URL helper*/
$this->load->helper('url');
redirect('https://www.goolge.com');
//redirect to google
}
}
Enter your redirect controller url in web browser like
https://www.example.com/
Enter the given URL to redirect your page on google.com
https://www.google.com/