Serj
Serj

Reputation: 41

CodeIgniter session is empty

I set a session in a class from libraries. Then I tried to get the value of this session in another class from libraries. The value is empty.

I took a look in CodeIgniter Library to find out what the problem could be and at the some similar topics from StackOverflow but I don't see anything wrong from my end. maybe I am missing something. Please help.

// IN THIS CLASS I SET THE SESSION 'fr_phone'

class Form_submit_fr {
    var $CI;
    protected $fr_id;
    protected $fr_data = array();
    public function __construct() {
      $this->CI =& get_instance();

      if (strpos($this->CI->uri->segment(1),'frid') !== false){ 
        $this->fr_id = (int)str_replace('frid','',$this->CI->uri->segment(1));

        $this->fr_data = $this->get_fr_data();

        // add the phone to session
        if (isset($this->fr_data->city_toll) && $this->fr_data->city_toll != '') {
            $fr_phone = $this->fr_data->city_toll;
          } else {
            $fr_phone = $this->fr_data->city_phone;
          }

          // here the session is set
          $this->CI->session->set_userdata('fr_phone', $fr_phone);

          //the result is correct
           echo $this->CI->session->userdata('fr_phone') . ' - fr_phone<br>';

      }
    }
 ....

}


// IN THIS CLASS I NEED TO GET THIS SESSION 'fr_phone'

class P_details {

    protected $page_link;
    protected $fr_data;

    var $CI;

    public function __construct(){
      $this->CI =& get_instance();
        if ($this->CI->uri->segment(2) == false && $this->CI->uri->segment(3) == false) {
          $this->page_link = $this->uri->segment(1)."/";
        } else if ($this->CI->uri->segment(1) != false && $this->CI->uri->segment(2) != false && $this->CI->uri->segment(3) == false) {

                     $this->page_link =  $this->CI->uri->segment(1)."/".$this->CI->uri->segment(2)."/";
        } else if ($this->CI->uri->segment(1) != false && $this->CI->uri->segment(2) != false && $this->CI->uri->segment(3) != false) {

          $this->page_link =  $this->CI->uri->segment(1)."/".$this->CI->uri->segment(2)."/";
          $this->fr_data = $this->get_fr_data($this->CI->uri->segment(3));

        }
      $this->CI->load->model('getDetails_model');


    }

    public function get_p_id($type = '') {


           $details_array = $this->CI->getDetails_model->all_details($this->page_link);

               if (empty($details_array)) {
                  return FALSE;
               }
               else {
                $data['fr_phone'] = '';

                $data['detail'] = $details_array;


                  // get phone from session 
                  // here the session is empty
                  echo $this->CI->session->userdata('fr_phone') . ' - fr_phone<br>';

                  if ($this->CI->session->userdata('fr_phone')) {
                    $data['fr_phone'] = $this->CI->session->userdata('fr_phone');
                  } 
                  if ($this->fr_data != '') {
                    $data['fr_detail'] = $this->fr_data;
                  }
                  return $data;
               }
    }

...

}

this is my config file:

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;


$config['cookie_prefix']    = '';
$config['cookie_domain']    = '';
$config['cookie_path']      = '/';
$config['cookie_secure']    = FALSE;
$config['cookie_httponly']  = FALSE;

this is my autoload file:

$autoload['libraries'] = array('database','session');

I have made the suggested changes in config file for $config['sess_save_path']. thanks a lot. I could see the files in the writable folder and the entries in database when I checked this way as well:

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 43200;
$config['sess_save_path'] = APPPATH.'writable';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

Also I used database for session:

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 43200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

In both cases the session works if I define a session in Controller in constructor or index function. I can get session in View files and in other Classes but if I set a session under a condition for a specific link then this session is always empty when I want to get it in a class from libraries folder or in view files:

//in controller
if (strpos($this->uri->segment(1),'frid') !== false){ 
   ...
$fr_phone = '111-222-3333';
  $this->session->set_userdata('fr_phone', $fr_phone);
   echo $this->session->userdata('fr_phone') // works 
}

When I call this session in a Class from libraries folder, the session is empty: above example for class P_details. And there is no way to get a session that is set in a Class from libraries folder and to get it in Another Class from libraries folder. It is very strange and I cannot get it. Also I tried to get session in a different controller and no success.

Upvotes: 0

Views: 1522

Answers (2)

Serj
Serj

Reputation: 41

It is very strange but I removed the session from autoload: $autoload['libraries'] = array('database'); I added session_start(); in controllers and I set the session in a common way:

$_SESSION['fr_phone'] = $fr_phone;

Then I was able to get this value in any files. Is this a bug? I tried without session_start() by adding $this->load->library('session'); and it did not work. Can anyone find an answer to this issue?

I am using CodeIgniter 3.0.2

Upvotes: 0

Reji kumar
Reji kumar

Reputation: 298

i had similar issue with one project. After long try i fix the issue the following way

1) in config.php

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'writable absolute path';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

and created the folder named "writable absolute path" in the root enter image description here

Hopes this will help

Upvotes: 1

Related Questions