Welcome Guest, Not a member yet? Register   Sign In
Reading a session variable in CI3 that was set by CI4
#1

Hello-  I am working on a large migration from CI3 to CI4.  I have attempted to set the session $savePath and $cookieName to be the same in CI3 and CI4, so that I can read in CI3 a session variable that is set by CI4.  My example is that I have a CI4 page which requires login.  It sets a session variable 'destination' with the URL to redirect to after logging in, then redirects (using a route) to the CI3 login page.  However, once there I am not able to retrieve that session variable.  I'm not sure what I am missing.
I changed app\Config\Session.php to have: 
Code:
    public string $cookieName = 'ci_session';
    public string $savePath = 'ci_sessions_CI3'; 
I confirmed that in CI3 application\config\config.php says:
Code:
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions_CI3';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

$config['cookie_prefix'] = '';
$config['cookie_domain'] = '';
$config['cookie_path'] = '/';
$config['cookie_secure'] = FALSE;
$config['cookie_httponly'] = FALSE;

My code in CI4 (app\Controllers\Manage\Admin.php)
Code:
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) {
        parent::initController($request, $response, $logger);
       
        $this->session = session();         
        $this->db = db_connect();
               
        //Check if this is being run via command line
        if (!$this->request->isCLI()){
            //a user must be logged in to go here so check if they are logged in and are an admin
            if(!$this->ionAuth->loggedIn()){   
                $this->session->set('message', 'Please log in to manage projects');
                $this->session->set('url', current_url()); 
                $this->session->set('destination', current_url());
                // use header because CI4 redirect needs to return and can't return in initController
                header("Location: " . site_url('auth/login'));
                exit;               
            }
            if (!$this->ionAuth->isAdmin()){
                show_404();
            }
        }       
    }

There is a route from auth/login that redirects to CI3 (application\controllers\Users.php). My CI3 code there looks like:  
Code:
        if($this->session->userdata('destination')!=""){
            $this->session->set_flashdata('destination',$this->session->userdata('destination'));
        }
        else{//If not, then just send them to profile management splash page on successful login
            $this->session->set_flashdata('destination',site_url('manage'));
        }
        redirect($this->session->flashdata('destination'), 'refresh');

The above code keeps redirecting me to site_url('manage').  
What am I missing to set the session var in CI4 and read it in CI3?  
Any help is appreciated, let me know if you have more questions.  Thank you!
Reply
#2

> The definition of the session table in Database Driver has changed.
See https://codeigniter4.github.io/CodeIgnit...en-changed
Reply
#3

(This post was last modified: 04-18-2024, 04:48 AM by xanabobana. Edit Reason: more detail )

Yes, I'm aware of that. I thought I had made all necessary changes.

If I

Code:
log_message('debug',log_message('debug', 'session ci4: ' .$_SESSION['destination']));

In CI4 the session is set.

When I read all sessions from CI3, after the page has redirected, the session I set is not there. 

I thought changing the savePath and cookieName in CI4 to match CI3 would take care of that change.  My syntax for setting / getting the session vars does seem to be correct in CI3 and CI4.  It's just that CI3 isn't seeing the session in CI4.  

    public string $savePath = 'ci_sessions_CI3'; 
Reply
#4

CI3 Session class cannot read CI4's Session data because the definitions of the session table are different.
Therefore, CI3 Session must be modified to be able to read CI4 Session data.
Reply
#5

Ah, thank you.  I changed my CI3 application/config/config.php to use  public string $savePath = 'ci_sessions_CI4'; and it worked!
Reply
#6

(04-18-2024, 04:31 AM)kenjis Wrote: CI3 Session class cannot read CI4's Session data because the definitions of the session table are different.
Therefore, CI3 Session must be modified to be able to read CI4 Session data.
Can you clarify this?  Are you saying that if CI3 is setup to read and write to the CI4 session table by using the same $cookieName and the same $savePath, then CI3 can read session info written by CI4?  Or, are you saying that the actual code for sessions in CI3 must be modified in order to read CI4 session data?

The comments in  CI4 app/Config/Encryption.php seem to imply that there are settings that will make it possible to read and write sessions that are compatible with CI3. For example: 
Code:
   
    /**
    * Whether the cipher-text should be raw. If set to false, then it will be base64 encoded.
    * This setting is only used by OpenSSLHandler.
    *
    * Set to false for CI3 Encryption compatibility.
    */
    public bool $rawData = true;
    ...

So, are there settings that can be used that will allow CI4 to read a session written by CI3 or write a session that CI3 can read?   That is what I am looking to do.  

BTW, the below did not work.
Reply
#7

The actual code for sessions in CI3 must be modified in order to read CI4 session data.
Reply
#8

(05-03-2024, 08:29 PM)kenjis Wrote: The actual code for sessions in CI3 must be modified in order to read CI4 session data.

Thank you for that clarification.  We were able to get this to work by modifying the CI4 code. CI4 was adding a prefix to  the session id which CI3 was not. In our case we use the database handler so we were able to modify this code in CI4 system/Session/Handlers/DatabaseHandler:

        // Add sessionCookieName for multiple session cookies.
        $this->idPrefix = $config->cookieName . ':';     
     
to this:       
        $this->idPrefix = '';   

After that the sessions were getting identified/stored with the same id's and both CI3 and CI4 could read the session.

I did not determine if the session info is being stored in an encrypted format but I changed all the settings in the CI4 app/Config/Encryption.php file to those that the comments said were compatible with CI3, and used the encryption key that was in the CI3 config files.
Reply
#9

when dynamically add select tag options via ajax in codeigniter 4.5 the session()->setFlashdata() is not working after submit the form why?

I can't show the Toast message to the user.

Let me know what's the alternative way to achieve it
Reply
#10

(05-07-2024, 10:14 PM)LP_bnss Wrote: when dynamically add select tag options via ajax in codeigniter 4.5 the session()->setFlashdata() is not working after submit the form why?

I can't show the Toast message to the user.

Let me know what's the alternative way to achieve it

Should this have been a new thread or are you trying to migrate from CI3 to CI4?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB