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


Messages In This Thread
Reading a session variable in CI3 that was set by CI4 - by xanabobana - 04-17-2024, 11:53 AM



Theme © iAndrew 2016 - Forum software by © MyBB