14

In my controller i used this way. i want to pass a variable data to my index function of the controller through redirect

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

and my index function is

function index($in)
{
    if($in==1)
    {

    }
}

But I'm getting some errors like undefined variables.
How can i solve this?

dev-masih
  • 4,188
  • 3
  • 33
  • 55
robins
  • 1,668
  • 6
  • 33
  • 69

6 Answers6

27

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')

Rejoanul Alam
  • 5,435
  • 3
  • 39
  • 68
  • It's a working way, but this should be possible to accomplish also with prettyurls and no session nor querystring. – Marc Compte Apr 26 '16 at 12:06
17

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.

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
Arizona2014
  • 1,317
  • 2
  • 18
  • 33
3

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');
Faridul Khan
  • 1,741
  • 1
  • 16
  • 27
3

If you want to complicate things, here's how:

On your routes.php file under application/config/routes.php, insert the code:

$route['home/index/(:any)'] = 'My_Controller/index/$1'; 

Then on your controller [My_Controller], do:

function index($in){
  if($in==1)
  {
     ...
  }
}

Finally, pass any value with redirect:

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

Keep up the good work!

3

I appreciate that this is Codeigniter 3 question, but now in 2021 we have Codeigniter 4 and so I hope this will help anyone wondering the same.

CI4 has a new redirect function (which works differently to CI3 and so is not a like for like re-use) but actually comes with the withInput() function which does exactly what is needed.

So to redirect to any URL (non named-routed) you would use:

return redirect()->to($to)->withInput();

In your controller - I emphasise because it cannot be called from libraries or other places.

In the function where you are expecting old data you can helpfully use the new old() function. So if you had a key in your original post of FooBar then you could call old('FooBar'). old() is useful because it also escapes data by default.

If however, like me, you want to see the whole post then old() isn't helpful as the key is required. In that instance (and a bit of a cheat) you can do this instead:

print'<pre>';print_r($_SESSION['_ci_old_input']['post']);print'</pre>';

CI4 uses the same flash data methods behind the scenes that were given in the above answers and so we can just pull out the relevant session data.

To then escape the data simply wrap it in the new esc() function.

Antony
  • 3,875
  • 30
  • 32
0

More info would be very helpful, as this should be working.

Things you can check:

  • Is your controller named home.php? Going to redirect(base_url()."home"); shows your home page?
  • Make your index function public.

    public function index($in) {
         ....
    }
    
Marc Compte
  • 4,579
  • 2
  • 16
  • 22