Codeigniter 3 - 从外部代码访问会话安装 [英] Codeigniter 3 - Access Session from Outside Codeigniter Installation

查看:144
本文介绍了Codeigniter 3 - 从外部代码访问会话安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法从我的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 在这里结合了答案:from Atiqur Ra​​hman Sumon

The original answer from @wolfgang1983 Ben Swinburne combinded with an answer here: from Atiqur Rahman Sumon

您可以将 index.php 但是,您需要更改 $ system_path $ application_folder 变量以匹配您的相对位置。如果你想完全改变你的整个应用程序的路径,但我不想,所以我只是把 index.php 文件复制到我需要的目录包含代码符号。

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

如果您现在刷新页面,则会加载默认控制器。 / strong>

If you refresh your page now, you would end up with the default controller loaded.

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

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 - 从外部代码访问会话安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆