CodeIgniter Forums
Sharing session with no CI app - 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: Sharing session with no CI app (/showthread.php?tid=80891)



Sharing session with no CI app - gurthang75 - 12-30-2021

Hi guys!
I´m having a hard time trying to share the same session between a native php app and a ci app the situation is the next:
We are currently trying to migrate an entire app to CI4
the native php app produce a session and at some time make a post to CI app when i land to the CI app (works great) the session does not pass to CI
im playing with the session files cause i think it search for the same ci_session name i already change the both sessions names to try to mach eachother (with no luck)
the session file that the old app is creating, got the name: sess_d5agcbecpo9jhq8951g59g67na
meanwhile when the app lands at ci this produce this file: mhp_session2ag2pf95sto44ukte3huptrt1gnap3k5  (mhp_session is my cookie_name in .env file)
Session are created anew.

Tip: when i click back in explorer it produce another session file with this name: sess_d5agcbecpo9jhq8951g59g67na (with no data and crash the sistem)

Another Tip in ci app im using session service from CI4 in my BaseController

Code:
$this->session = \Config\Services::session();

Thanks in advance!


RE: Sharing session with no CI app - ikesela - 12-30-2021

check your CI session driver, default is FileHandler (store in writable folder)


RE: Sharing session with no CI app - InsiteFX - 12-31-2021

I have my BaseController like this for checking session.

PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\CLIRequest;
use 
CodeIgniter\HTTP\IncomingRequest;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Config\Services;
use 
Psr\Log\LoggerInterface;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *    class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 */
class BaseController extends Controller
{
    /**
    * Instance of the main Request object.
    *
    * @var CLIRequest|IncomingRequest
    */
    protected $request;

    /**
    * An array of helpers to be loaded automatically upon
    * class instantiation. These helpers will be available
    * to all other controllers that extend BaseController.
    *
    * @var array
    */
 
protected $helpers = [];

 
/**
 * @var \CodeIgniter\Session\Session
 */
 
protected $session;

 
/**
 * @var array - Global data array for views.
 *
 * Fill in your global view data here that will be
 * used in all views.
 */
 
protected static $globalViewData = [
 
'test' => 'This works!',
 ];

    /**
    * Constructor.
    */
    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
 
    // Do Not Edit This Line
 
    parent::initController($request$response$logger);

 
    // Preload any models, libraries, etc, here.

 
    // E.g.: $this->session = \Config\Services::session();

 
    // Ensure that the session is started and running
 
    if (session_status() == PHP_SESSION_NONE)
 
    {
 
    // $this->session = \Config\Services::session();
 
    $this->session Services::session();
 
    }
    }

 
/**
 * mergeGlobalData ()
 * -------------------------------------------------------------------
 *
 * You must set the globalViewData array above with your global data
 * for all Controllers.
 *
 * Usage:
 *
 * $data = $this->mergeGlobalData($data);
 * return view('your_view', $data);
 *
 * @param  array $viewData
 * @return array
 */
 
protected function mergeGlobalData(array $viewData) : array
 {
 if (
is_array(self::$globalViewData))
 {
 return 
array_merge($viewDataself::$globalViewData);
 }

 return [];
 }

 
// -------------------------------------------------------------------

}  // End of BaseController class.

/**
 * -----------------------------------------------------------------------
 * Filename: BaseController.php
 * Location: ./app/Controllers/BaseController.php
 * -----------------------------------------------------------------------
 */ 

Make sure you do the session check to see if it is loaded first.


RE: Sharing session with no CI app - gurthang75 - 01-03-2022

Hi boyos! 
first of all thks for the response!
now... i tried the session check first in my base controller -> didnt work... its still creating a new session file in writable/session folder i change my .env to try and match the name of my session with the other app like this:
Code:
app.sessionDriver = 'CodeIgniter\Session\Handlers\FileHandler'
app.sessionCookieName = 'sess'

the result is: a session file named sess_aj2fri8h0ucnsfu5shkjhmmtaj -> my app session
sess7mo31q37chk4ghtbkjl81i8ruhlg2id7 my ci session if i putt the sessionCookieName = 'sess_' generates a session with the name "sess_" diferente to the other app named "sess", gives me two session diferents names....

Im using filehnadler, the two sessions are stored in writable/session folder
Salute!


RE: Sharing session with no CI app - InsiteFX - 01-04-2022

Best bet would be to store the information in the database. Sharing session data would be a security risk.