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

acu*_*joe 5 php session codeigniter

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

ROOT /
     .. /application
     .. /system
     .. /includes
        .. /Events.php <- I need access from here
Run Code Online (Sandbox Code Playgroud)

理论上,下面的代码应该起作用,至少根据其他答案,因为新的CI会话库传递给本地会话.

session_id($_COOKIE['ci_session']);
session_start();
var_dump($_SESSION); // returns null
Run Code Online (Sandbox Code Playgroud)

我是误会吗?

acu*_*joe 7

来自@ wolfgang1983 Ben Swinburne的原始答案结合答案:来自Atiqur Ra​​hman 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
Run Code Online (Sandbox Code Playgroud)

/includes/index.php:

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

//$application_folder = 'application';
$application_folder = '../application';
Run Code Online (Sandbox Code Playgroud)

现在,您可以在文件中包含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');
?>
Run Code Online (Sandbox Code Playgroud)

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

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

ob_start();
define("REQUEST", "external"); <--
include('index.php');
ob_end_clean();
Run Code Online (Sandbox Code Playgroud)

在你的default_controller.php:

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

    //other code for normal requests.
}
Run Code Online (Sandbox Code Playgroud)