![]() |
Reading a session variable in CI3 that was set by CI4 - 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: Reading a session variable in CI3 that was set by CI4 (/showthread.php?tid=90683) Pages:
1
2
|
Reading a session variable in CI3 that was set by CI4 - xanabobana - 04-17-2024 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'; Code: $config['sess_driver'] = 'database'; My code in CI4 (app\Controllers\Manage\Admin.php) Code: public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { 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')!=""){ 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! RE: Reading a session variable in CI3 that was set by CI4 - kenjis - 04-17-2024 > The definition of the session table in Database Driver has changed. See https://codeigniter4.github.io/CodeIgniter4/installation/upgrade_sessions.html#what-has-been-changed RE: Reading a session variable in CI3 that was set by CI4 - xanabobana - 04-18-2024 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'; RE: Reading a session variable in CI3 that was set by CI4 - kenjis - 04-18-2024 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. RE: Reading a session variable in CI3 that was set by CI4 - xanabobana - 04-18-2024 Ah, thank you. I changed my CI3 application/config/config.php to use public string $savePath = 'ci_sessions_CI4'; and it worked! RE: Reading a session variable in CI3 that was set by CI4 - ngv - 04-22-2024 (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.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: 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. RE: Reading a session variable in CI3 that was set by CI4 - kenjis - 05-03-2024 The actual code for sessions in CI3 must be modified in order to read CI4 session data. RE: Reading a session variable in CI3 that was set by CI4 - ngv - 05-07-2024 (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. RE: Reading a session variable in CI3 that was set by CI4 - LP_bnss - 05-07-2024 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 RE: Reading a session variable in CI3 that was set by CI4 - Bosborne - 05-08-2024 (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? Should this have been a new thread or are you trying to migrate from CI3 to CI4? |