gpt4 book ai didi

php - Codeigniter 3 - 从外部 Codeigniter 安装访问 session

转载 作者:可可西里 更新时间:2023-11-01 13:47:02 26 4
gpt4 key购买 nike

我似乎无法将 session 数据从我的 codeigniter 应用程序传递回我的 includes 文件夹中的脚本。根据我阅读的其他答案,我需要设置我的 session_id() 才能使用 session_start() 重新加入 session 。

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

理论上,下面的代码应该可以工作,至少根据其他答案是这样,因为新的 CI session 库会传递给 native session 。

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

我是不是误解了 session ?

最佳答案

来自@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');
?>

如果您现在刷新页面,您将加载默认 Controller 。

因此,根据 Atiqur Ra​​hman Sumon 的回答,我们可以在加载之前定义一个常量来告诉默认 Controller 我们要跳过它的正常调用堆栈。

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.
}

关于php - Codeigniter 3 - 从外部 Codeigniter 安装访问 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31006756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com