how to use redirection in codeigniter?

Page Redirection in Codeigniter

we often need to redirect the user from one page to another page. The redirect() function is used for this purpose. it is used to "header redirect" to the specified URL.

Note : $this->load->helper('url'); // URL helper must be loaded before redirect() function used.

Syntax :
redirect($uri = '', $method = 'auto', $code = NULL)

Parameters

$uri (string) − URI string

$method (string) − Redirect method (‘auto’, ‘location’ or ‘refresh’)

$code (string) − HTTP Response code (usually 302 or 303)

Return type : void

The first argument can have two types of URI. We can pass full site URL or URI segments to the controller you want to direct.

The second optional parameter can have any of the three values from auto, location or refresh. The default is auto.

The third optional parameter is only available with location redirects and it allows you to send specific HTTP response code.


Example :-

Create a controller called Redirect_controller.php and save it in application/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 some site*/
redirect('http://www.tutorialspoint.com');
}

public function computer_graphics() {
/*Load the URL helper*/
$this->load->helper('url');
redirect('http://www.tutorialspoint.com/computer_graphics/index.htm');
}

public function version2() {
/*Load the URL helper*/
$this->load->helper('url');

/*Redirect the user to some internal controller’s method*/
redirect('redirect/computer_graphics');
}

}
?>

Change the routes.php file in application/config/routes.php to add route for the above controller and add the following line at the end of the file.

$route['redirect'] = 'ittutorial_controller';
$route['redirect/show'] = 'ittutorial_controller/show';
$route['redirect/showlist'] = 'ittutorial_controller/showlist';
Type the following URL in the browser, to execute the example.

http://itttutorial.in/index.php/redirect
The above URL will redirect you to the itechtuto.com website and if you visit the following URL, then it will redirect you to the computer graphics tutorial at itechtuto.com.

http://itttutorial.com/index.php/redirect/showlist


function process() {
$this->load->library('form_validation');

[...]

if ($this->form_validation->run() == FALSE) {
$this->load->view('login', $this->data);
}
else {
// Go to private area
redirect('home', 'refresh');
}
}

$this->load->helper('url');
if (condition == TRUE) {
redirect('new_page');
}

How to redirect url in codeigniter?

if ($user_logged_in === FALSE)
{
redirect('home');
redirect('home/method_name');
redirect(base_url(), 'refresh');
redirect(base_url('login'), 'refresh');
redirect(base_url('controller/method'), 'refresh');
redirect(base_url().'controller/method', 'refresh');
}

How to redirect url in codeigniter?

redirect('ittuttorial/user/applicant_detail/'.base64_encode($_SESSION['UserID']));





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here