【问题标题】:How to check session/userdata from one codeigniter app to another codeigniter app如何检查从一个 codeigniter 应用程序到另一个 codeigniter 应用程序的会话/用户数据
【发布时间】:2016-02-09 07:35:46
【问题描述】:

我有 2 个 Codeigniter 应用程序,我们称它们为 CI-A 和 CI-B。

  • CI-A 未使用会话库。
  • CI-B 使用会话库。
  • CI-A 和 CI-B 都在使用 Codeigniter 3.x。
  • CI-A 和 CI-B 文件都放在同一个服务器和同一个域中。

如何让 CI-A 检查 CI-B 中是否存在活动会话/用户数据?

谢谢!

【问题讨论】:

  • @codeHeart 我去看看。谢谢。
  • 您是否尝试将会话保存在数据库中?
  • @MohsenShakibafar 是的,这正是我正在尝试的。

标签: php codeigniter session


【解决方案1】:

我自己解决这个问题。

首先,确保将会话数据存储在数据库中。不是文件。在这种情况下,CI-A 和 CI-B 都使用同一个数据库。

其次,在 CI-B 上你需要获取 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

第三,在CI-A上你需要验证CI-B提交的$key

$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;
}

如果您发现任何错误,请对以上代码进行更正。谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 2017-01-30
    相关资源
    最近更新 更多