CodeIgniter Forums
Accessing CI session data outside of CI - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Accessing CI session data outside of CI (/showthread.php?tid=71829)



Accessing CI session data outside of CI - happyape - 09-28-2018

I have a folder in my
Code:
Codeigniter4/public/oldproject

where i have created a test.php so that I can access it using
PHP Code:
myci4site.com/oldproject/test.php 

In test.php

PHP Code:
$path realpath(dirname(dirname(dirname(__FILE__))).'/writable/session');
//ini_set('session.save_path',$path);
session_save_path ($path);
session_id$_COOKIE['ci4session'] );
session_start();

print_r($_SESSION); 
but it prints an empty array.

There is session data in the session which I can print in my Home controller e.g. myci4site.com/home/test which just contains

PHP Code:
$sess session()->get();
echo 
'<pre>';print_r($sess); 

What else I need to do to be able to get session data in my test.php?

I need to access this data for maintaining old code for the time being.
Thanks.


RE: Accessing CI session data outside of CI - skunkbad - 09-28-2018

If you review CI’s session class you will see that it is much more complex in configuring the session. You cannot simply access the session data without duplicating much of what the session class is doing.


RE: Accessing CI session data outside of CI - happyape - 09-29-2018

Hmm - I don't want to duplicate code etc and was hoping for an easier/quick fix.


RE: Accessing CI session data outside of CI - skunkbad - 09-29-2018

Depending on what handler/driver you will be using, using CI4's session outside of CI may not be that hard to do, as long as your are using PSR-4 autoloading. Looks like you just new up a driver (injecting config), and inject it into the session class constructor. There's probably a little more to it, but you shouldn't have to reinvent the wheel.


RE: Accessing CI session data outside of CI - happyape - 09-29-2018

(09-29-2018, 05:49 AM)skunkbad Wrote: Depending on what handler/driver you will be using, using CI4's session outside of CI may not be that hard to do, as long as your are using PSR-4 autoloading. Looks like you just new up a driver (injecting config), and inject it into the session class constructor. There's probably a little more to it, but you shouldn't have to reinvent the wheel.

Yes!. Thank you, I'll give it a go.