Codeigniter 3 - Access Session from Outside Codeigniter Installation
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.
}
access codeigniter session values from external files
<?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');
?>
Codeigniter Session Outside the application folder
It is possible, but not without some work. You can see the details of someone else's problem and their solution in the CodeIgniter forum.
Basically you need take the cookie that CodeIgniter uses to handle it's sessions and unserialize it:
$sess = unserialize($_COOKIE['ci_session']);
You may need to also change settings in your application so that cookies are set for the entire domain, not just for the folder that CodeIgniter sits in. Is there any way to get session data in folder outside Application folder of Codeigniter?
The current Session_native.php doesn't seem to change any of the built in session library's preference or interfere with how the session data is saved, i think the following should work:
- Get a hold of the session id for the session you want to load
- call
session_id($sessid)
with this session id beforesession_start()
- call
session_start()
session.save_path
is the same and maybe (if your host have this extension installed) suhoshin settings like suhosin.session.cryptdocroot
doesn't interfere. Accessing CodeIgniter super object from external php script outside codeigniter installation
CI's main index.php
sets the paths of the system and application folders. If you are including index.php
from another directory, these paths are set relative to your "including" directory.
Try changing the following lines in index.php
$system_path = 'system';
// change to...
$system_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'system';
// and...
$application_folder = 'application';
// change to...
$application_folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'application';
The dirname(__FILE__)
will give you the absolute path of the index.php
even if you include it somewhere else.
Related Topics
PHP Splitting an Array into Two Arrays - Keys Array and Values Array
PHP Redirect with Http Query String Variables
How to Get an Array of Data from $_Post
How Does PHP Max_Execution_Time Work
"Call to Undefined Function" Error When Calling Class Method
Error with .Htaccess and Mod_Rewrite
Selenium2 Firefox: Use the Default Profile
Zend Framework - Multiplate Navigation Blocks
Check If a String Is All Caps in PHP
Rounding Up to the Second Decimal Place
Random Float Number Between 0 and 1.0 PHP
PHP Local Server Invalid Request (Unexpected Eof)
Send Zip File to Browser/Force Direct Download
How to Convert Latin1_Swedish_Ci Data into Utf8_General_Ci