Codeigniter 3 - session access from external Codeigniter installation
I cannot get the session data passed from my codeigniter app back to the script in my include folder. From what I've read in other answers, I need to set mine session_id()
to get back to the session with session_start()
.
ROOT /
.. /application
.. /system
.. /includes
.. /Events.php <- I need access from here
In theory, the code below should work, at least according to the other answers, as the new CI session library is going to native sessions.
session_id($_COOKIE['ci_session']);
session_start();
var_dump($_SESSION); // returns null
Don't I understand the questions?
source to share
Original answer by @ wolfgang1983 Ben Swinburne combined with the answer here: by Atikur Rahman Sumon
You can include index.php
from any directory, however you need to change the variables $system_path
and $application_folder
according to your relative location. Well, great if you want to completely change all your application paths, but I didn't want to, so I just copied the file index.php
into the directory where I needed to include the 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:
<?php
ob_start();
include('index.php');
ob_end_clean();
$CI =& get_instance();
$CI->load->library('session'); //if it not autoloaded in your CI setup
echo $CI->session->userdata('name');
?>
If you refreshed your page now, you would end up loading the default controller.
So, taking the answer from Atiqur Rahman Sumon, we can define a constant before loading to tell the default controller that we want to skip its normal column.
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.
}
source to share
I found this code to access code value from external files , it might help you after.
<?php
ob_start();
include('index.php');
ob_end_clean();
$CI =& get_instance();
$CI->load->library('session'); //if it not autoloaded in your CI setup
echo $CI->session->userdata('name');
?>
source to share
Improving on @acupajoe's answer, you don't need to copy-paste the CI index.php
. Change the part include
to this instead :
<?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 the value index.php
:
$system_path = isset($temp_system_path) ? $temp_system_path : 'system';
and
$application_folder = isset($temp_application_folder) ? $temp_application_folder : 'application';
source to share