2015-06-23 40 views
3

我似乎无法获得从我的codeigniter应用程序传递回我的includes文件夹中的脚本的会话数据。从我读过的其他答案中,我需要将我的session_id()设置为能够重新加入session_start()的会话。Codeigniter 3 - 从外部Codeigniter安装访问会话

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

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

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

我误会吗?

+0

[从外部文件访问笨会话值]的可能的复制(HTTP:/ /stackoverflow.com/questions/7926455/access-codeigniter-session-values-from-external-files) –

回答

4

从@ 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'; 

现在,您可以与您的文件笨:

<?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的回答中,我们可以定义一个常量,以告诉默认控制器我们想要跳过它的正常调用堆栈。

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

这个答案的帽子。真正保存了大量的混淆,为什么一个AJAX请求加载我们的应用程序的登录屏幕的HTML! – PaulSkinner

+0

@PaulSkinner很高兴能帮到你! – acupajoe

+0

对于那些想将CI用作其后端框架的人来说,这是最好的解决方案:)。像我一样,我需要使用WordPress作为我的前端,我需要一个硬代码添加一个新的登录仪表板,这将发生。 –

0

提高对@ 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';