Codeigniter 3 - 从外部 Codeigniter 安装访问会话 [英] Codeigniter 3 - Access Session from Outside Codeigniter Installation
问题描述
我似乎无法将从我的 codeigniter 应用程序传递回我的包含文件夹中的脚本的会话数据.根据我对其他答案的了解,我需要设置我的 session_id()
以便能够使用 session_start()
重新加入会话.
I can't seem to get session data passed from my codeigniter application back to a script in my includes folder. From what I've read on other answers, I need to set my session_id()
to be able to rejoin a session with session_start()
.
ROOT /
.. /application
.. /system
.. /includes
.. /Events.php <- I need access from here
理论上,下面的代码应该可以工作,至少根据其他答案,因为新的 CI 会话库会传递给本机会话.
Theoretically the code below should work, at least according to other answers, because the new CI session library passes on to native sessions.
session_id($_COOKIE['ci_session']);
session_start();
var_dump($_SESSION); // returns null
我是否误解了会话?
推荐答案
来自@wolfgang1983 的原始答案 Ben Swinburne 合并这里有一个答案:来自 Atiqur Rahman Sumon
The original answer from @wolfgang1983 Ben Swinburne combined with an answer here: from Atiqur Rahman Sumon
您可以包含任何目录中的 index.php
,但是,您需要更改 $system_path
和 $application_folder
变量以匹配你的相对位置.好吧,如果您想完全更改整个应用程序的路径,那就太好了,但我不想这样做,所以我只是将 index.php
文件复制到我需要包含 codeigniter 的目录中.>
You can include the index.php
from any directory, however, you need to change the $system_path
and $application_folder
variables to match your relative location. Well that's great if you want to completely change your whole application's paths, but I didn't want to, so I just copied the index.php
file into the directory I needed to include codeigniter with.
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:
Now you can include codeigniter in your file with the:
<?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 Rahman Sumon 的回答中,我们可以在加载之前定义一个常量来告诉默认控制器我们要跳过它的正常调用堆栈.
So taking from Atiqur Rahman Sumon's answer, we can define a constant before load to tell the default controller we want to skip it's normal 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.
}
这篇关于Codeigniter 3 - 从外部 Codeigniter 安装访问会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!