How to transfer redirected data to codeigniter

In my controller, I used this method. I want to pass variable data to my controller index function via redirection

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

and my index function

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

But I get some errors like undefined variables.
How can i solve this?

+10
source share
4 answers

So, in the controller you can have one function:

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

And in the objective function, you can access the value of $ in as follows:

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

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

Hope this helps.

+6
source

Use a session to transfer data when redirecting. CodeIgniter has a special method called "set_flashdata"

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

Now you can get in in the pointer controller, for example

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

Remember that this data will be available only for redirection and loss at the next page request. If you need stable data, you can use the URL with the parameter and GET $this->input->get('param1')

+18
source

More information would be very helpful, as this should work.

Things you can check:

  • Is your controller the name home.php? Transition to redirect(base_url()."home"); shows your homepage?
  • Make your index feature public.

     public function index($in) { .... } 
0
source

Use a session to transfer data during redirection. There are two steps

Step 1 (Post Function):

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

Step 2 (redirection function):

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

Source: https://habr.com/ru/post/989466/


All Articles