2

I have 2 Codeigniter applications, lets call them CI-A and CI-B.

  • CI-A not using session library.
  • CI-B using session library.
  • Both CI-A and CI-B are using Codeigniter 3.x.
  • Both CI-A and CI-B files are placed in same server and same domain.

How I can make CI-A check if there is active session/userdata in CI-B?

Thanks!

mokalovesoulmate
  • 271
  • 2
  • 6
  • 18

1 Answers1

0

I am solving this by myself.

First, make sure you are storing the session data in database. Not files. In this case, both CI-A and CI-B are using same database.

Second, on CI-B you need to get cookie ID

$this->load->helper('cookie');
$cookie_name = $this->config->item('sess_cookie_name'); //cookie name
$key = $this->input->cookie($cookie_name); //get cookie id

Third, on CI-A you need to validate the $key submitted by CI-B.

$save_path = $this->config->item('sess_save_path'); //session table name

$this->load->helper('cookie');
$key = $this->input->post('key'); //submitted from CI-B

//dirty solution
$this->db->where('id', $key);
$query = $this->db->get($save_path);

$data = $query->row();

if (!empty($data)) {
    //session exist
    //code below are from external source. Even the author were saying it is a horrible solution: http://forum.codeigniter.com/thread-61330.html
    $session_data = $data->data;

    $return_data = array();

    $offset = 0;
    while ($offset < strlen($session_data)) {
    if (!strstr(substr($session_data, $offset), "|")) {
        throw new Exception("invalid data, remaining: " . substr($session_data, $offset));
    }
    $pos = strpos($session_data, "|", $offset);
    $num = $pos - $offset;
    $varname = substr($session_data, $offset, $num);
    $offset += $num + 1;
    $data = unserialize(substr($session_data, $offset));
    $return_data[$varname] = $data;
    $offset += strlen(serialize($data));
    }
    return $return_data;
} else {
    //session not exist
    return FALSE;
}

Please make a correction to above code if you find any mistake. Thanks!

mokalovesoulmate
  • 271
  • 2
  • 6
  • 18