6

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

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

Am I misunderstanding sessions?

1

3 Answers 3

8

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

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

In /includes/index.php:

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

//$application_folder = 'application';
$application_folder = '../application';

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

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

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();

And in your default_controller.php:

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

    //other code for normal requests.
}
4
  • 1
    Hats off for this answer. Genuinely saved a great deal of confusion as to why an AJAX request was loading the login screen's HTML of our app! Jun 10, 2016 at 13:53
  • @PaulSkinner glad I could help!
    – acupofjose
    Jun 10, 2016 at 16:28
  • 1
    this is the best solution for those who want to use CI as their backend framework :). Like me i need to use wordpress as my front end and i need a hard code one to add a new login dashboard and this will take place. Dec 28, 2016 at 7:17
  • 1
    Best answer. Still valid for CodeIgniter Version 3.1.9
    – mutiemule
    Dec 23, 2018 at 22:11
3

Improving on @acupajoe's answer, you don't have to copy-paste the CI index.php. Instead change the include part into this:

<?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();
    //...
?>

Then change in index.php:

$system_path = isset($temp_system_path) ? $temp_system_path : 'system';

and

$application_folder = isset($temp_application_folder) ? $temp_application_folder : 'application';
1
  • Thumbs up for define("REQUEST", "external");. Add this line and your site won't redirect to other pages if your index.php always redirecting to other pages.
    – Frank
    Sep 15, 2018 at 2:17
1

I found this access codeigniter session values from external files it may help what you after.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.