Himanshu Goyal Himanshu Goyal - 1 year ago 46
PHP Question

how to save session value and pass it from one function of controller to another function

Sending a value from one function of my controller 'login_user' to another function 'dashboard' the value is saved in a session but i cant access the value when i'm using the uri routing to get it here is the code

public function login_user(){
//$cache=$this->input->post();
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
$this->form_validation->set_error_delimiters("<p class='text-danger'>","</p>");
if($this->form_validation->run()){
$email=$this->input->post('email');
$password=$this->input->post('password');
$this->load->model('Pmodel');
$login_data=$this->Pmodel->login_model($email,$password);
print_r($login_data);
if($login_data){
$data= $this->session->set_userdata('data', $login_data );
var_dump($data);
return redirect(base_url()."user/dashboard".$data);
//$this->load->view('dashboard/dashboard',['data'=>$data]);
}else{
$this->session->set_flashdata('Login_failed','Invalid Username/Password.');
return redirect('user/login_invalid');
//echo "string";
}
}else{
return redirect('user/login_invalid');
}
}


And another function

public function dashboard(){
if ($this->session->userdata('data') === NULL){
echo "hello";
return redirect('user/login_invalid');
}else{
$login_data = $this->uri->segment(3);
var_dump($login_data);
$this->load->model('Pmodel');
$email=$this->Pmodel->select_model('$email');
$this->load->view('dashboard/dashboard',$email);
}
}

Answer Source

Just call below function instead of redirection if both functions "login_user" and "dashboard" are in same controller.

$data= $this->session->set_userdata('data', $login_data );
var_dump($data);   
return redirect(base_url()."user/dashboard".$data);

Replace above redirection with simple class function call with:

$data= $this->session->set_userdata('data', $login_data );
$this->dashboard($data);

The dashboard function body may look like this now.

function dashboard($data=null){
  // your code here
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download