How to pass a data with redirect in codeigniter

So in the controller you can have in one function :

$in=1;
redirect(base_url()."home/index/".$in);

And in the target function you can access the $in value like this :

$in = $this->uri->segment(3);   
if(!is_numeric($in))
{
  redirect();       
}else{
   if($in == 1){

   }
}

I put segment(3) because on your example $in is after 2 dashes. But if you have for example this link structure : www.mydomain.com/subdomain/home/index/$in you'll have to use segment(4).

Hope that helps.


Use session to pass data while redirecting. There are a special method in CodeIgniter to do it called "set_flashdata"

$this->session->set_flashdata('in',1);
redirect("home/index");

Now you may get in at index controller like

function index()
{
 $in = $this->session->flashdata('in');
 if($in==1)
  {

  }
}

Remember this data will available only for redirect and lost on next page request. If you need stable data then you can use URL with parameter & GET $this->input->get('param1')


Use session to pass data while redirecting.There are two steps

Step 1 (Post Function):

  $id = $_POST['id']; 
  $this->session->set_flashdata('data_name', $id);
  redirect('login/form', 'refresh');

Step2 (Redirect Function):

  $id_value = $this->session->flashdata('data_name');