Codeigniter 3 - 从外部Codeigniter安装访问会话

时间:2015-06-23 15:15:12

标签: php codeigniter session

我似乎无法将从我的codeigniter应用程序传递的会话数据返回到我的includes文件夹中的脚本。根据我在其他答案中阅读的内容,我需要设置session_id()以便能够与session_start()重新加入会话。

ROOT /
     .. /application
     .. /system
     .. /includes
        .. /Events.php <- I need access from here

理论上,至少根据其他答案,下面的代码应该有效,因为新的CI会话库会传递给本机会话。

session_id($_COOKIE['ci_session']);
session_start();
var_dump($_SESSION); // returns null

我是否误解了会议?

3 个答案:

答案 0 :(得分:7)

@ wolfgang1983 Ben Swinburne的原始答案加上答案:来自Atiqur Rahman Sumon

您可以在任意目录中添加index.php,但是,您需要更改$system_path$application_folder变量以匹配您的相对位置。好吧,如果你想完全改变你的整个应用程序的路径,但我不想这样做,所以我只是将index.php文件复制到我需要包含codeigniter的目录中。

ROOT /
     .. /application
     .. /system
     .. /includes
        .. /Events.php <- I need access from here
        .. /index.php <- Copied CI index with new paths
     .. /index.php

/includes/index.php

//$system_path = 'system';
$system_path = '../system';

//$application_folder = 'application';
$application_folder = '../application';

现在,您可以在文件中包含codeigniter:

<?php
    ob_start();
    include('index.php');
    ob_end_clean();
    $CI =& get_instance();
    $CI->load->library('session'); //if it's not autoloaded in your CI setup
    echo $CI->session->userdata('name');
?>

如果您现在刷新页面,最终会加载默认控制器。

因此,从Atiqur Ra​​hman Sumon的回答中,我们可以在加载之前定义一个常量来告诉默认控制器我们想跳过它的正常callstack。

ob_start();
define("REQUEST", "external"); <--
include('index.php');
ob_end_clean();

在您的default_controller.php

function index()
{
    if (REQUEST == "external") {
        return;
    } 

    //other code for normal requests.
}

答案 1 :(得分:2)

改进@ acupajoe的答案,您不必复制粘贴CI index.php。而是将include部分更改为:

<?php
    ob_start();
    define("REQUEST", "external");
    $temp_system_path = 'path/to/system/folder/from/external/file';
    $temp_application_folder = 'path/to/application/folder/from/external/file';
    include('path/to/index.php/file/from/external/file');
    ob_end_clean();
    $CI =& get_instance();
    //...
?>

然后更改index.php

$system_path = isset($temp_system_path) ? $temp_system_path : 'system';

$application_folder = isset($temp_application_folder) ? $temp_application_folder : 'application';

答案 2 :(得分:0)

我发现这个access codeigniter session values from external files可能对你有所帮助。

<?php
    ob_start();
    include('index.php');
    ob_end_clean();
    $CI =& get_instance();
    $CI->load->library('session'); //if it's not autoloaded in your CI setup
    echo $CI->session->userdata('name');
?>
相关问题