Page redirecting in CodeIgniter
0 5117
- Sometimes we need to redirect a page to another page in our project.
- In native PHP, we use the header() method for redirection.
- Codeigniter provides a redirect() method to redirect a page to another page.
Related Topics:
Codeigniter Interview Questions
Page caching in Codeigniter
Common functions in Codeigniter
Page Redirecting in CodeIgniter
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:
Step 1 Open the application/controllers directory and create a new controller Redirect_controller.php.
<?php
class Redirect_controller extends CI_Controller {
public function index() {
/*Load the URL helper*/
$this->load->helper('url');
/*Redirect the user to CodingTag site*/
redirect('https://www.codingtag.com');
}
public function codeigniter() {
/*Load the URL helper*/
$this->load->helper('url');
redirect('https://www.codingtag.com/codeigniter-introduction'); //redirect to codeigniter tutorials on CodingTag
}
}
?>
Step 2 Enter the given URL into the browser to redirect to your page on codingtag.com
http://localhost/ci/index.php/Redirect_controller
Step 3 Enter the given URL to redirect your page on Codeigniter tutorials on codingtag.com
http://localhost/ci/index.php/Redirect_controller/codeigniter
Share:
Comments
Waiting for your comments